package EGS;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.ExecutorService;

import javax.swing.JTable;

import org.dom4j.Document;
import org.dom4j.Element;

import LOGISTICS.c_material;
import LOGISTICS.m_material;
import ORDER.c_order;
import ORDER.m_orderBOM;
import ORDER.orderManagement;
import PRODUCT.PACKAGE.c_package;
import TOOLS.SQLpool;
import TOOLS.SQLtool;
import TOOLS.errCodeManagement;
import TOYS.LASERUPH.laserUPHreporter;
import WORKPLACE.m_SNinfo;
import WORKPLACE.m_panelSonResultState;
import WORKPLACE.m_resultData;
import WORKPLACE.stationClient;

@SuppressWarnings("unused")
public class EGS_execute240923 {
	public boolean isUnknowCase = false;
	ExecutorService iExecutorService;
	Document MSG;
	boolean isPanel = false;
	String gName=null;
	stationClient iClient=null;
	SQLtool sql = null;
	public EGS_execute240923(stationClient nClient, Document MSG_in, ExecutorService executorService, 
			SQLtool goldenSql,String threadGname) throws Exception {
		super();
		gName=threadGname;
		sql=goldenSql;
		if(nClient!=null) {
			iClient=nClient;
		}else {
			iClient=null;
		}
		MSG = MSG_in;
		iExecutorService = executorService;
	}
	
	public void close() {
//		try {
//			sql.close();//mySql = goldenSql;不用关闭
//		} catch (Exception e) {
//			;
//		}
//		try {
//			iClient.close();
//		} catch (Exception e) {
//			;
//		}
	}

	public Document runCal() {
		String threadName =gName;
//		isUnknowCase=false;
		String detailLog = "";
		String msgClass = "";
		try {
//			if(iClient!=null) {
//				iClient.setSQL(mySql);
//			}
			if (MSG == null) {
				return null;
			}
			Element Mroot = MSG.getRootElement();
			msgClass = Mroot.element("header").attribute("messageClass").getValue();
			String transactionID = Mroot.element("header").attribute("transactionID").getValue();
			String iStation, iSN, iLayer, iState;
			int iiLayer, iiState = 0;

			threadName = threadName + "_" + msgClass;
			iStation = Mroot.element("header").element("location").attribute("equipmentName").getValue();
			threadName = threadName + "_" + iStation;
			Thread.currentThread().setName(threadName);

			String endT = "", startT1 = GetTimestamp();
			long diffMS = 0;

			if ((msgClass.equals("501")) || (msgClass.equals("5018"))) {
				/**
				 * 卡前站 1、获取工位工序 2、查询条码卡前站情况 2.1、未注册条码异常：查询工位当前工单及其工艺，如果是前工序都是非必过站，进行工单注册
				 */
				iSN = Mroot.element("body").element("pcb").attribute("barcode").getValue();
				iLayer = Mroot.element("body").element("pcb").attribute("pcbSide").getValue();
				String isPanel="-1";
				try {
					isPanel = Mroot.element("body").element("pcb").attribute("isPanel").getValue();
				}catch(Exception ee) {
					isPanel="-1";
				}
				String procedure="-1";
				try {
					procedure = Mroot.element("body").element("pcb").attribute("procedureNumber").getValue();
					if(procedure.length()<1) {
						procedure="-1";
					}
				}catch(Exception ee) {
					procedure="-1";
				}
				iiLayer = Integer.parseInt(iLayer);
				
				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				
				try {
					if(iiLayer==-1) {
						iiLayer=iClient.myLayer;
					}
					if (iiLayer != iClient.myLayer) {
						String str = iStation + "工单激活面别("+iClient.myLayer+")与产品上传面别不一致！";
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", str);
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(Integer.parseInt(msgClass + "0"), "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
					} else {
						boolean result = iClient.checkWIPpermmision(iSN,isPanel,procedure); 
//						boolean result = true;
//						endT = GetTimestamp();
//						diffMS = getDiffMS(startT1, endT);
//						System.out.println("_"+diffMS);
						String errorCode, errorText, actionCode;
						if (result) {
							errorCode = "0";
							errorText = "Valid";
							actionCode = "0";
							if (iClient.myProcedureNumber.equals("A0200")) {// 检查是否有标签，等何工布置后解除屏蔽
								String iLab = sql.selectFirstValue("select label from history_snLabel where state=1 "
										+ "and serialNumber='" + iSN + "'");
								if ((iLab != null) && (!iLab.equals(iSN))) {
									String sss="select os.serialNumber,ll.labelPart,ll.autoLabel from vOrderSerialNumber os \r\n" + 
											"LEFT JOIN vOrderList_new op ON op.orderNumber=os.orderNumber \r\n" + 
											"left JOIN vOrderList_new om ON om.orderNumber=op.masterOrderNumber\r\n" + 
											"LEFT JOIN LabelList ll ON ll.LabelPart=om.partNumber and ll.autoLabel=2\r\n" + 
											"where os.serialNumber='"+iSN+"' and ll.autoLabel=2 ";
									JTable t=sql.select(sss);//标签如果没有二维码，不必强制扫描二维码
									if(t==null || t.getRowCount()<1) {
										errorCode = "1";
										errorText = iSN + "此产品全检要求扫描产品标签";
										actionCode = "0";
										result = false;
									}
								}
							}
//							if ((iStation.indexOf("L01-NPM") > 0) || (iStation.indexOf("L02-NPM") > 0)
//									|| (iStation.indexOf("L03-NPM") > 0) || (iStation.indexOf("L05-NPM") > 0)
//									|| (iStation.indexOf("L08-NPM") > 0) || (iStation.indexOf("L23-NPM") > 0)) {
//								iClient.uploadPanelState(iSN, true,"-1");
//							}//软件已切换成赢哥的，不需要按501补550
						} else {
							errorCode = "1";
							errorText = iSN + "前工序异常";
							actionCode = "0";
						}
						eleR.addAttribute("errorCode", errorCode);
						eleR.addAttribute("errorText", errorText);
						eleR.addAttribute("actionCode", actionCode);
						AKSLogs(Integer.parseInt(msgClass + "1"),
								"" + iSN + "_" + iStation + "_" + iLayer + "_" + result);
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					endT = GetTimestamp();
					long detailT = getDiffMS(startT1, endT);
//					detailLog = "1stCheckState" + detailT + "ms";
//					System.out.println(detailLog+"_"+e.getMessage());
					if (e.getMessage().equals("-152")) {
						try {
							String realSN = iClient.getSN4Label(iSN);// 如果条码未识别，先查询看看是否为标签
							endT = GetTimestamp();
							detailT = getDiffMS(startT1, endT);
							detailLog = detailLog + ",2stCheckLabel" + detailT + "ms";
							if (realSN != null) {
//								System.out.println(realSN);
								try {
									boolean result = iClient.checkWIPpermmision(realSN,isPanel,procedure);
									String errorCode, errorText, actionCode;
									if (result) {
										errorCode = "0";
										errorText = "Valid";
										actionCode = "0";
//										if ((iStation.indexOf("L01-NPM") > 0) || (iStation.indexOf("L02-NPM") > 0)
//												|| (iStation.indexOf("L03-NPM") > 0)
//												|| (iStation.indexOf("L05-NPM") > 0)
//												|| (iStation.indexOf("L08-NPM") > 0)
//												|| (iStation.indexOf("L23-NPM") > 0)) {
//											iClient.uploadPanelState(iSN, true,"-1");
//										}//软件已切换成赢哥的，不需要按501补550
									} else {
										errorCode = "1";
										errorText = iSN + "前工序异常";
										actionCode = "0";
									}
									eleR.addAttribute("errorCode", errorCode);
									eleR.addAttribute("errorText", errorText);
									eleR.addAttribute("actionCode", actionCode);
									AKSLogs(Integer.parseInt(msgClass + "1"), "" + iSN + "_" + iStation + "_" + iLayer);
								} catch (Exception e1) {
									String errorCode, errorText, actionCode;
									errorCode = "1";
									errorText = iSN + "_" + errCodeManagement.getErrText(e1.getMessage());
									actionCode = "0";
									eleR.addAttribute("errorCode", errorCode);
									eleR.addAttribute("errorText", errorText);
									eleR.addAttribute("actionCode", actionCode);
//									System.out.println("labelCheck:" + errorText);
									AKSLogs(Integer.parseInt(msgClass + "0"),
											"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorText);
								}
							} else {
								String errorCode, errorText, actionCode;
								errorCode = "1";
								errorText = iSN + "_条码不存在";
								actionCode = "0";
								eleR.addAttribute("errorCode", errorCode);
								eleR.addAttribute("errorText", errorText);
								eleR.addAttribute("actionCode", actionCode);
//								System.out.println("labelCheck:" + errorText);
								AKSLogs(Integer.parseInt(msgClass + "0"),
										"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorText);
							}
						} catch (Exception e1) {
							String errorCode, errorText, actionCode;
							errorCode = "1";
							errorText = iSN + "_" + errCodeManagement.getErrText(e1.getMessage());
							actionCode = "0";
							eleR.addAttribute("errorCode", errorCode);
							eleR.addAttribute("errorText", errorText);
							eleR.addAttribute("actionCode", actionCode);
//							System.out.println("labelCheck:" + errorText);
							AKSLogs(Integer.parseInt(msgClass + "0"),
									"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorText);
						}
					} else {
						str=e.getMessage();
						String [] sstr=e.getMessage().split("@");
						if(sstr[0].equals("-153")) {
							str=iSN + "_" + sstr[1]
							+"_工位工单="+iClient.activeOrderNumber;
						}else {
							str = iSN + "_" + errCodeManagement.getErrText(e.getMessage());
						}
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", str);
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(Integer.parseInt(msgClass + "0"), "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
					}
				}
			} else if (msgClass.equals("502")) {
				/**
				 * 出站检查 1、获取工位工序 2、查询条码本工序情况
				 */
				iSN = Mroot.element("body").element("pcb").attribute("barcode").getValue();
				iLayer = Mroot.element("body").element("pcb").attribute("pcbSide").getValue();
				iiLayer = Integer.parseInt(iLayer);
				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				try {
					if (iiLayer != iClient.myLayer) {
						String str = "工单激活面别与产品上传面别不一致！";
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", str);
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(Integer.parseInt(msgClass + "0"), "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
						return MSG;
					}
					boolean result = iClient.checkWIPfinish(iSN);
//					System.out.println("" + iSN + "_" + iStation + "_" + iLayer + "_" + result);
					String errorCode, errorText, actionCode;
					if (result) {// 本工序已Pass
						errorCode = "0";
						errorText = "Valid";
						actionCode = "0";
					} else {// 本工序未做
						errorCode = "1";
						errorText = "本工序未执行";
						actionCode = "0";
					}
					eleR.addAttribute("errorCode", errorCode);
					eleR.addAttribute("errorText", errorText);
					eleR.addAttribute("actionCode", actionCode);
					AKSLogs(Integer.parseInt(msgClass + "1"),
							"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorCode);
				} catch (Exception e) {// 本工序Fail
					String errorCode, errorText, actionCode;
					if (e.getMessage().equals("NGfinish")) {
						errorCode = "2";
						errorText = "本工序Fail";
						actionCode = "0";
						AKSLogs(Integer.parseInt(msgClass + "1"),
								"" + iSN + "_" + iStation + "_" + iLayer + "_" + errorCode);
					} else {
						String str = errCodeManagement.getErrText(e.getMessage());
						errorCode = "3";
						errorText = str;
						actionCode = "0";
						AKSLogs(Integer.parseInt(msgClass + "0"), "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
					}
					eleR.addAttribute("errorCode", errorCode);
					eleR.addAttribute("errorText", errorText);
					eleR.addAttribute("actionCode", actionCode);
				}
			} else if (msgClass.equals("10018")) {// 511 物料信息查询
				iSN = Mroot.element("body").element("material").attribute("id").getValue();
				String isOffLine = Mroot.element("body").element("offline").attribute("value").getValue();
				try {
					Mroot.remove(Mroot.element("body"));
					Mroot.addElement("body");
				} catch (Exception e1) {
					;
				}
				Element eleR = Mroot.element("body").addElement("result");
				try {
					SimpleDateFormat fm = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					c_material cm = new c_material(sql);
					m_material iReel = null;
					try {
						iReel = cm.getMaterial(iSN);
					} catch (Exception e) {
						if (e.getMessage().equals("-174")) {// 获取不到就100004来凑
							String[] pickOrder = null;
							Vector<String> inputs = new Vector<String>();
							isPanel = false;
							JTable t = sql.select("select orderNumber from assetStation where ID='" + iStation + "'");
							if (t == null || t.getRowCount() < 1) {
								throw e;
							}
							String oOrder = (String) t.getValueAt(0, 0);
							Thread iTH = new Thread(new Runnable() {
								String orderNo = oOrder;
								@Override
								public void run() {
									String str = "select masterOrderNumber from vOrderList_new "
											+ "where panelOrderNumber='" + orderNo + "' group by masterOrderNumber";
									try {
										JTable t = sql.select(str);
										if (t != null && t.getRowCount() > 0) {
											isPanel = true;
											for (int i = 0; i < t.getRowCount(); i++) {
												String iOrder = (String) t.getValueAt(i, 0);
												inputs.add(iOrder);
											}
										}
									} catch (Exception e) {
										;
									}
								}
							});

							Thread iTH1 = new Thread(new Runnable() {
								String orderNo = oOrder;
								@Override
								public void run() {
									String str = "select masterOrderNumber from vOrderList_new "
											+ "where masterOrderNumber='" + orderNo + "' group by masterOrderNumber";
									try {
										JTable t = sql.select(str);
										if (t != null && t.getRowCount() > 0) {
											for (int i = 0; i < t.getRowCount(); i++) {
												String iOrder = (String) t.getValueAt(i, 0);
												inputs.add(iOrder);
											}
										}
									} catch (Exception e) {
										;
									}
								}
							});
							iTH.start();
							iTH1.start();

							iTH.join();
							if (isPanel == false) {
								iTH1.join();
							}

							if (oOrder==null || oOrder.length()<1) {
								throw new Exception("当前工位"+iStation+"未激活工单");
							}
							
							if (inputs.isEmpty()) {
								throw new Exception("工单" + oOrder + "不存在");
							}

							pickOrder = new c_material(sql).getPickListOrders_mergeNo(inputs);
							if (pickOrder == null) {
								throw new Exception("WMS工单" + inputs + "不存在");
							}
							Vector<String> sonOrders = new Vector<String>();
							String strPickOrders = "";
							for (int i = 0; i < pickOrder.length; i++) {
								if (pickOrder[i] != null && pickOrder[i].length() > 0
										&& sonOrders.indexOf(pickOrder[i]) < 0) {
									sonOrders.add(pickOrder[i]);
									if (sonOrders.size() == 1) {
										strPickOrders = "'" + pickOrder[i] + "'";
									} else {
										strPickOrders = strPickOrders + ",'" + pickOrder[i] + "'";
									}
								}
							}
							strPickOrders = "(" + strPickOrders + ")";
							new c_material(sql).getPickResult_groups(strPickOrders);
							iReel = cm.getMaterial(iSN);
						} else {
							throw e;
						}
					}
					if (isOffLine.equals("0")) {
						if (iReel.partType.MSDvalidity > 0 && iReel.partType.MSDvalidity < 525600) {// 单位分钟，MDS大于零且小于一年的需要按MSD管理
							if (iReel.MSD_openTime == null) {
								throw new Exception("-175");
							}

						}
					}
					Element elePreReel = null;
					Element elePreReelDT = null;
					if ((iReel.preReel != null) && (iReel.preReel.length() > 1)) {
						List<m_material> preReelList = new ArrayList<m_material>();
						Vector<String> preReels = new Vector<String>();
						m_material reelCounter = iReel;
						preReels.add(iReel.materialID);
						while ((reelCounter.preReel != null) && (reelCounter.preReel.length() > 1)) {
							if (preReels.indexOf(reelCounter.preReel) < 0) {
								preReels.add(reelCounter.preReel);
								reelCounter = cm.getMaterial(reelCounter.preReel);
								preReelList.add(reelCounter);
							} else {
								reelCounter.preReel = "";
							}
						}
						int preLen = preReelList.size();
						for (int il = 0; il < preLen; il++) {
							elePreReel = Mroot.element("body").addElement("material");
							m_material preReel = preReelList.get(preLen - il - 1);
							elePreReel.addAttribute("id", preReel.materialID);
							elePreReel.addAttribute("state", "0");
							if (preReel.MSD_openTime != null) {
//								long remainTime = (preReel.MSD_invalidTime.getTime() - new Date().getTime()) / 1000/ 60;
								long remainTime = (preReel.MSD_invalidTime.getTime() - new Date().getTime()) / 1000;
								if (remainTime == 0) {
									remainTime = -1;
								}
								elePreReel.addAttribute("remainTime", "" + remainTime);
								elePreReel.addAttribute("unpackDate", fm.format(preReel.MSD_openTime));
							} else {
								elePreReel.addAttribute("remainTime", "0");
								elePreReel.addAttribute("unpackDate", "");
							}
							elePreReel.addAttribute("prevReelId", preReel.preReel);
							elePreReel.addAttribute("splicedReelId", preReel.nextReel);
							elePreReelDT = elePreReel.addElement("detail");
							elePreReelDT.addAttribute("part", preReel.partType.part.partNumber);
							elePreReelDT.addAttribute("Opart", preReel.partType.Opart.partNumber);
							elePreReelDT.addAttribute("qty", "" + preReel.quantity);
							elePreReelDT.addAttribute("lot", preReel.lotNumber);
							elePreReelDT.addAttribute("vendor", preReel.supplyerPart);
							elePreReelDT.addAttribute("userdata", "");
							elePreReelDT.addAttribute("partclass", "");
							elePreReelDT.addAttribute("msdlevel", preReel.partType.MSDlevel);
							elePreReelDT.addAttribute("standardTime", "" + preReel.partType.MSDvalidity);
							elePreReelDT.addAttribute("lowerlimit", "" + preReel.partType.MSDthreshold);
						}
					}
					Element eleIReel = Mroot.element("body").addElement("material");
					eleIReel.addAttribute("id", iReel.materialID);
					eleIReel.addAttribute("state", "0");
					if (iReel.MSD_openTime != null) {
//						long remainTime = (iReel.MSD_invalidTime.getTime() - new Date().getTime()) / 1000 / 60;
						long remainTime = (iReel.MSD_invalidTime.getTime() - new Date().getTime()) / 1000 ;
						if (remainTime == 0) {
							remainTime = -1;
						}
						eleIReel.addAttribute("remainTime", "" + remainTime);
						eleIReel.addAttribute("unpackDate", fm.format(iReel.MSD_openTime));
					} else {
						eleIReel.addAttribute("remainTime", "0");
						eleIReel.addAttribute("unpackDate", "");
					}
					eleIReel.addAttribute("prevReelId", iReel.preReel);
					eleIReel.addAttribute("splicedReelId", iReel.nextReel);
					Element eleIReelDT = eleIReel.addElement("detail");
					eleIReelDT.addAttribute("part", iReel.partType.part.partNumber);
					eleIReelDT.addAttribute("Opart", iReel.partType.Opart.partNumber);
					eleIReelDT.addAttribute("qty", "" + iReel.quantity);
					eleIReelDT.addAttribute("lot", iReel.lotNumber);
					eleIReelDT.addAttribute("vendor", iReel.supplyerPart);
					eleIReelDT.addAttribute("userdata", "");
					eleIReelDT.addAttribute("partclass", "");
					eleIReelDT.addAttribute("msdlevel", iReel.partType.MSDlevel);
					eleIReelDT.addAttribute("standardTime", "" + iReel.partType.MSDvalidity);
					eleIReelDT.addAttribute("lowerlimit", "" + iReel.partType.MSDthreshold);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100181, "" + iSN + "_" + iStation);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(100180, "" + iSN + "_" + iStation + "_" + str);
				}
			} else if (msgClass.equals("520")) {// 520 物料装载_备料_必须有台车元素
				String str = Mroot.element("body").element("operation").attribute("type").getValue();
				if (str.length() < 1) {
					str = "1";
				}
				String isReplace = str;
				String op = Mroot.element("body").element("operation").attribute("operator").getValue();
				String time = Mroot.element("body").element("operation").attribute("timestamp").getValue();
				String cartID = "";
				try {
					cartID = Mroot.element("body").element("cart").attribute("ID").getValue();
				} catch (Exception e3) {
					;
				}
				if (cartID == null || cartID.length() < 1) {
					cartID = "NA";
				}
				String iMaterialID = Mroot.element("body").element("material").attribute("id").getValue();
				String pos = Mroot.element("body").element("position").attribute("p1").getValue();
				String p1 = "-1", p2 = "-1", p3 = "-1";
				try {
					p1 = Mroot.element("body").element("position").attribute("p2").getValue();
				} catch (Exception e) {
					;
				}
				try {
					p2 = Mroot.element("body").element("position").attribute("p3").getValue();
				} catch (Exception e) {
					;
				}
				try {
					p3 = Mroot.element("body").element("position").attribute("p4").getValue();
				} catch (Exception e) {
					;
				}
				try {
					str = Mroot.element("body").element("position").attribute("feederID").getValue();
					if (str == null || str.length() < 1) {
						str = "-1";
					}
				} catch (Exception e) {
					str = "-1";
				}
				String feeder = str;

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				String iiMaterialID = iMaterialID;// 电文里申请安装的料
				String soMaterialID = null;// 设备当前已安装的料
				/**
				 *  设备当前已安装的料
				 */
				String sMaterialID = null;// 设备当前已安装的料
				m_material nReel = null;// 设备当前已安装的料的最后序的料
				try {

					Vector<String> runningReels = new Vector<String>();
					c_material cm = new c_material(sql);
					if (isReplace.equals("2")) {// 如果要接料，先查当前料
						str = "select reel from setupLocation " + "where station='" + iStation + "' and location='"
								+ pos + "' and p1='" + p1 + "' and p2='" + p2 + "' and p3='" + p3 + "'";
						JTable tt = sql.select(str);
						if (tt != null && tt.getRowCount() > 0) {
							soMaterialID = (String) tt.getValueAt(0, 0);
							sMaterialID = soMaterialID;
							if (sMaterialID != null && sMaterialID.length() > 0) {
								nReel = cm.getMaterial(sMaterialID);
								runningReels.add(sMaterialID);
								while (nReel.nextReel.length() > 0) {
									try {
										if (runningReels.indexOf(nReel.nextReel) < 0) {
											runningReels.add(nReel.nextReel);
											nReel = new c_material(sql).getMaterial(nReel.nextReel);
										} else {
											nReel.nextReel = "";
										}
									} catch (Exception e3) {
										nReel.nextReel = "";
									}
								}
								sMaterialID = nReel.materialID;
								if(runningReels.indexOf(iiMaterialID)>=0) {
									throw new Exception("禁止重复接料");
								}
							} else {
								isReplace = "1";
							}
						} else {
							isReplace = "1";
						}
					}

					// 查询料盘信息，位置、数量、接料
					m_material iReel = cm.getMaterial(iMaterialID);
					if (iReel.quantity < 1) {
						throw new Exception("-179");
					}
					if(iReel.location!=null && iReel.location.equals("WMS")) {
						throw new Exception("-185");//该物料已退库未领料
					}
					int preC = 0;
					Vector<String> setupReels = new Vector<String>();
					setupReels.add(iMaterialID);
					m_material rrc=null;
					while (iReel.preReel.length() > 0) {
						try {
							if (setupReels.indexOf(iReel.preReel) < 0) {
								rrc=iReel;
								iReel = new c_material(sql).getMaterial(iReel.preReel);
								if(iReel.quantity>0) {
									setupReels.add(iReel.materialID);
									preC += 1;
								}else {
									iReel=rrc;
									iReel.preReel = "";
								}
								
							} else {
								iReel.preReel = "";
							}
						} catch (Exception e3) {
							iReel.preReel = "";
						}
					}
					str = "";
					if (!iReel.materialID.equals(iMaterialID)) {
						str = "扫描Reel:" + iMaterialID + "带出的第" + preC + "前序物料";
					}
					if (!iReel.materialID.equals(sMaterialID)) {
						String tips = str;
						String iisReplace = isReplace;
						String ssMaterialID = sMaterialID;
						String icartID = cartID;
						m_material ioReel = iReel;
						Thread iTH = new Thread(new Runnable() {
							int istate = Integer.parseInt(iisReplace);
							String iOP = op;
							String iTime = time;
							m_material iiReel = ioReel;
							String oMaterial = iiMaterialID;
							String sMaterial = ssMaterialID;
							String iiStation = iStation;
							String iicartID = icartID;
							String iLocation = pos;
							String iFeeder = feeder;
							String iTips = tips;
							@Override
							public void run() {
								try {
									String mlocation = iiStation + "@" + iicartID + "@" + iLocation;
									c_material cmt = new c_material(sql);
									try {
										String oOrder=getStationOrder(iiStation);
										cmt.logMaterial(istate, iOP, iTime, iiReel.materialID, iiReel.quantity,
												iiStation, iicartID + "@" + iLocation, iFeeder, iTips,oOrder);
										if (istate == 2) {// 接料
											String str = "update materialID set preReel='" + sMaterial
													+ "' where materialID='" + iiReel.materialID + "'";
											sql.Command(str);
											str = "update materialID set nextReel='" + iiReel.materialID
													+ "' where materialID='" + sMaterial + "'";
											sql.Command(str);
										}
										if(!iiReel.location.equals(mlocation)) {
											sql.Command("update materialID set location='lineBIN' " + "where location='"
													+ mlocation + "'");
											sql.Command("update materialID set location='" + mlocation
													+ "' where materialID='" + oMaterial + "'");
										}
									} catch (Exception e3) {
//										String str=errCodeManagement.getErrText(e3.getMessage());
										;
									}
								} catch (Exception e2) {
//									String str=errCodeManagement.getErrText(e2.getMessage());
									;
								}
//								try {
//									iisql.close();
//								}catch(Exception e1) {
//									;
//								}
							}
						});

						// TODO 检查feeder是否处在空闲位置，待开发

						if (isReplace.equals("1")) {// 换料
							str = "update setupLocation set cartID='" + cartID + "' ,feeder='" + feeder + "',reel='"
									+ iReel.materialID + "',nextReel='" + iReel.nextReel + "',quantity="
									+ iReel.quantity + ",lastEdit=GETDATE() " + "where station='" + iStation
									+ "' and location='" + pos + "' and p1='" + p1 + "' and p2='" + p2 + "' and p3='"
									+ p3 + "'";
							try {
								sql.Update(str);
							} catch (Exception e3) {
								if (e3.getMessage().equals("-116")) {
									str = "insert setupLocation (station,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity) values "
											+ "('" + iStation + "','" + cartID + "','" + pos + "','" + p1 + "','" + p2
											+ "','" + p3 + "','" + feeder + "','" + iReel.materialID + "','"
											+ iReel.nextReel + "'," + iReel.quantity + ")";
									sql.Command(str);
								} else {
									throw e3;
								}
							} // 更新物料所在位置、feeder所在位置
						} else if (isReplace.equals("2")) {
							if (!iReel.partType.part.partNumber.equals(nReel.partType.part.partNumber)) {
								throw new Exception("-178");
							}
							if (sMaterialID.equals(soMaterialID)) {
								sql.Command("update setupLocation set nextReel='" + iReel.materialID
										+ "',lastEdit=getDate() where reel='" + sMaterialID + "'");
							}
						}
						iTH.start();

					} else {
						if (isReplace.equals("2")) {
							throw new Exception("-182");
						}
					}

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5201, "" + iiMaterialID + "_" + cartID + "_" + iStation);
				} catch (Exception e) {
					str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5200, "" + iiMaterialID + "_" + cartID + "_" + iStation + "_" + str);
				}
			} else if (msgClass.equals("521")) {// 521 物料装载_备料_必须有台车元素
				String str = Mroot.element("body").element("operation").attribute("type").getValue();
				if (str.length() < 1) {
					str = "1";
				}
				String isReplace = str;
				String op = Mroot.element("body").element("operation").attribute("operator").getValue();
				String time = Mroot.element("body").element("operation").attribute("timestamp").getValue();
				String cartID = "";
				try {
					cartID = Mroot.element("body").element("cart").attribute("ID").getValue();
				} catch (Exception e3) {
					;
				}
				if (cartID == null || cartID.length() < 1) {
					cartID = "NA";
				}
				String iMaterialID = Mroot.element("body").element("material").attribute("id").getValue();
				String pos = Mroot.element("body").element("position").attribute("p1").getValue();
				String p1 = "-1", p2 = "-1", p3 = "-1";
				try {
					p1 = Mroot.element("body").element("position").attribute("p2").getValue();
				} catch (Exception e) {
					;
				}
				try {
					p2 = Mroot.element("body").element("position").attribute("p3").getValue();
				} catch (Exception e) {
					;
				}
				try {
					p3 = Mroot.element("body").element("position").attribute("p4").getValue();
				} catch (Exception e) {
					;
				}
				try {
					str = Mroot.element("body").element("position").attribute("feederID").getValue();
					if (str == null || str.length() < 1) {
						str = "-1";
					}
				} catch (Exception e) {
					str = "-1";
				}
				String feeder = str;

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				String iiMaterialID = iMaterialID;
				String soMaterialID = null;// 设备当前已安装的料
				String sMaterialID = null;// 设备当前已安装的料
				m_material nReel = null;// 设备当前已安装的料的最后序的料
				try {

					c_material cm = new c_material(sql);
					if (isReplace.equals("2")) {// 如果要接料，先查当前料
						str = "select reel from setupLocation " + "where station='" + iStation + "' and location='"
								+ pos + "' and p1='" + p1 + "' and p2='" + p2 + "' and p3='" + p3 + "'";
						JTable tt = sql.select(str);
						if (tt != null && tt.getRowCount() > 0) {
							soMaterialID = (String) tt.getValueAt(0, 0);
							sMaterialID = soMaterialID;
							if (sMaterialID != null && sMaterialID.length() > 0) {
								nReel = cm.getMaterial(sMaterialID);
								Vector<String> runningReels = new Vector<String>();
								runningReels.add(sMaterialID);
								while (nReel.nextReel.length() > 0) {
									try {
										if (runningReels.indexOf(nReel.nextReel) < 0) {
											runningReels.add(nReel.nextReel);
											nReel = new c_material(sql).getMaterial(nReel.nextReel);
										} else {
											nReel.nextReel = "";
										}
									} catch (Exception e3) {
										nReel.nextReel = "";
									}
								}
								sMaterialID = nReel.materialID;
								if(runningReels.indexOf(iiMaterialID)>=0) {
									throw new Exception("禁止重复接料");
								}
							} else {
								isReplace = "1";
							}
						} else {
							isReplace = "1";
						}
					}

					// 查询料盘信息，位置、数量、接料
					m_material iReel = cm.getMaterial(iMaterialID);
					if (iReel.quantity < 1) {
						throw new Exception("-179");
					}
					if(iReel.location!=null && iReel.location.equals("WMS")) {
						throw new Exception("-185");//该物料已退库未领料
					}
					int preC = 0;
					Vector<String> setupReels = new Vector<String>();
					setupReels.add(iMaterialID);
					while (iReel.preReel.length() > 0) {
						try {
							if (setupReels.indexOf(iReel.preReel) < 0) {
								setupReels.add(iReel.preReel);
								iReel = new c_material(sql).getMaterial(iReel.preReel);
								preC += 1;
							} else {
								iReel.preReel = "";
							}
						} catch (Exception e3) {
							iReel.preReel = "";
						}
					}
					str = "";
					if (!iReel.materialID.equals(iMaterialID)) {
						str = "扫描Reel:" + iMaterialID + "带出的第" + preC + "前序物料";
					}
					if (!iReel.materialID.equals(sMaterialID)) {
						String tips = str;
						String iisReplace = isReplace;
						String ssMaterialID = sMaterialID;
						String icartID = cartID;
						m_material ioReel = iReel;
						Thread iTH = new Thread(new Runnable() {
							int istate = Integer.parseInt(iisReplace);
							String iOP = op;
							String iTime = time;
							m_material iiReel = ioReel;
							String oMaterial = iiMaterialID;
							String sMaterial = ssMaterialID;
							String iiStation = iStation;
							String iicartID = icartID;
							String iLocation = pos;
							String iFeeder = feeder;
							String iTips = tips;

							@Override
							public void run() {
								try {
									String mlocation = iiStation + "@" + iicartID + "@" + iLocation;
									c_material cmt = new c_material(sql);
									try {
										String oOrder=getStationOrder(iiStation);
										cmt.logMaterial(istate, iOP, iTime, iiReel.materialID, iiReel.quantity,
												iiStation, iicartID + "@" + iLocation, iFeeder, iTips,oOrder);
										if (istate == 2) {// 接料
											sql.Command("update materialID set preReel='" + sMaterial
													+ "' where materialID='" + iiReel.materialID + "'");
											sql.Command("update materialID set nextReel='" + iiReel.materialID
													+ "' where materialID='" + sMaterial + "'");
										}
										if(!iiReel.location.equals(mlocation)) {
											sql.Command("update materialID set location='lineBIN' " + "where location='"
													+ mlocation + "'");
											sql.Command("update materialID set location='" + mlocation
													+ "' where materialID='" + oMaterial + "'");
										}
									} catch (Exception e3) {
//										String str=errCodeManagement.getErrText(e3.getMessage());
										;
									}
								} catch (Exception e2) {
//									String str=errCodeManagement.getErrText(e2.getMessage());
									;
								}
//								try {
//									iisql.close();
//								}catch(Exception e1) {
//									;
//								}
							}
						});

						// TODO 检查feeder是否处在空闲位置，待开发

						if (isReplace.equals("1")) {// 换料
							str = "update setupLocation set cartID='" + cartID + "' ,feeder='" + feeder + "',reel='"
									+ iReel.materialID + "',nextReel='" + iReel.nextReel + "',quantity="
									+ iReel.quantity + ",lastEdit=GETDATE() " + "where station='" + iStation
									+ "' and location='" + pos + "' and p1='" + p1 + "' and p2='" + p2 + "' and p3='"
									+ p3 + "'";
							try {
								sql.Update(str);
							} catch (Exception e3) {
								if (e3.getMessage().equals("-116")) {
									str = "insert setupLocation (station,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity) values "
											+ "('" + iStation + "','" + cartID + "','" + pos + "','" + p1 + "','" + p2
											+ "','" + p3 + "','" + feeder + "','" + iReel.materialID + "','"
											+ iReel.nextReel + "'," + iReel.quantity + ")";
									sql.Command(str);
								} else {
									throw e3;
								}
							} // 更新物料所在位置、feeder所在位置
						} else if (isReplace.equals("2")) {
							if (!iReel.partType.part.partNumber.equals(nReel.partType.part.partNumber)) {
								throw new Exception("-178");
							}
							if (sMaterialID.equals(soMaterialID)) {
								sql.Command("update setupLocation set nextReel='" + iReel.materialID
										+ "',lastEdit=getDate() where reel='" + sMaterialID + "'");
							}
						}
						iTH.start();

					}

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5211, "" + iiMaterialID + "_" + cartID + "_" + iStation);
				} catch (Exception e) {
					str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5210, "" + iiMaterialID + "_" + cartID + "_" + iStation + "_" + str);
				}
			} else if (msgClass.equals("540") || msgClass.equals("602")) {
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				eleR.addAttribute("errorCode", "0");
				eleR.addAttribute("errorText", "Valid");
				eleR.addAttribute("actionCode", "0");
				isUnknowCase = true;
//				AKSLogs(Integer.parseInt(msgClass+"1"), "" +  "_" + iStation);
			} else if (msgClass.equals("550")) {
				String gXML=Mroot.asXML();
				iSN = Mroot.element("body").element("panel").attribute("pcbID").getValue();
				iLayer = Mroot.element("body").element("panel").attribute("productSide").getValue();
				iState = Mroot.element("body").element("panel").attribute("state").getValue();
				String iRepair = "0";
				try {
					iRepair = Mroot.element("body").element("panel").attribute("repairType").getValue();
				} catch (Exception e) {
					iRepair = "0";
				}
				String procedure="-1";
				try {
					procedure = Mroot.element("body").element("panel").attribute("procedureNumber").getValue();
					if(procedure.length()<1) {
						procedure="-1";
					}
				}catch(Exception ee) {
					procedure="-1";
				}
				if(procedure.equals("-1")) {
					procedure=iClient.myProcedureNumber;
				}
				Element elePanel = Mroot.element("body").element("panel");
				List<?> sons = elePanel.elements();
				iiLayer = Integer.parseInt(iLayer);
				Mroot.element("body").remove(Mroot.element("body").element("panel"));
				Element eleR = Mroot.element("body").addElement("result");
//				if (iClient.myProcedureNumber.equals("A0200")) {//  后续要弄成标准化的可配置项
//					
//				}else {
//					iClient.orderActiveRequired = true;
//				}//2023-03-27屏蔽,已经弄成可按设备编号配置项
				boolean isget = false;
				try {
					iiState = Integer.parseInt(iState);
					isget = true;
				}catch(Exception e) {
					String str = errCodeManagement.getErrText("过站电文异常body->panel->state_"+e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5500, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
//				endT = GetTimestamp();
//				diffMS = getDiffMS(startT1, endT);
				if(isget && (iiLayer==-1)) {
					iiLayer=iClient.myLayer;
				}
				if (!isget) {
					;
				} else if (iiLayer != iClient.myLayer) {
					String str = "工单激活面别(" + iClient.myLayer + ")与产品上传面别不一致！";
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5500, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
				} else {
					boolean bState = false;
					if (iiState == 0) {
						bState = true;
					}
					boolean bRepair = false;
					if (iRepair.equals("1")) {
						bRepair = true;
					}
					try {
						boolean icallXRay = false;
						if (
//								(sons.size() <= 1) && 
								(!iClient.isPanel)) {//如果是拼板，即便只有一个子板也按拼板模式分析
							if (sons.size() >0) {
								List<?> errComps = sons;
								Vector<String> iRpComps = new Vector<String>();
								for (int n = 0; n < errComps.size(); n++) {
									String isRpass = "0";
									try {
										isRpass = ((Element) errComps.get(n)).attribute("bRpass").getValue();
									}catch(Exception e) {
										isRpass = ((Element) errComps.get(n)).attribute("Rpass").getValue();
									}
									if (isRpass.equals("1")) {
										String[] strComps = ((Element) errComps.get(n)).attribute("comps").getValue()
												.split(",");
										iRpComps.addAll(Arrays.asList(strComps));
									}
								}
								if (iRpComps.size() > 0) {
									String iMasterNumber=null;//  
									String isqlstr="select masterNumber from vOrderListPanel where "
											+ "panelOrderNumber='" + iClient.activeOrderNumber + "'";
									JTable t1=sql.select(isqlstr);
									if(t1!=null && t1.getRowCount()>0) {
										iMasterNumber = ""+t1.getValueAt(0, 0);
									}
									
									if (iMasterNumber != null && iMasterNumber.length() > 0) {// 查询总成号
										JTable t = sql.select(
												"select partNumber,comps,materialNumber from SPIxRayList where partNumber='"
														+ iMasterNumber + "'");
										if (t != null && t.getRowCount() > 0) {// 下载加严清单中该总成的所有位号
											Vector<String> iXRComps = new Vector<String>();
											for (int m = 0; m < t.getRowCount(); m++) {
												String strXR = "" + t.getValueAt(m, 1);
												if (strXR != null && strXR.length() > 0 && (!strXR.equals("null"))) {
													iXRComps.addAll(Arrays.asList(strXR.split(",")));
												}
											}
											if (iXRComps.size() > 0) {
												for (int m = 0; m < iRpComps.size(); m++) {// 验证是否在加严清单中
													String strr = iRpComps.get(m);
													if (strr != null && strr.length() > 0) {
														if (iXRComps.indexOf(iRpComps.get(m)) >= 0) {
															icallXRay = true;
															m = iRpComps.size() + 1;
														}
													}
												}
											}
										}
									}
								}
							}
							try {
								iClient.uploadState(iSN, bState, bRepair, icallXRay,procedure);// 如果工序是A0200强制重回FCT，除非没有FCT工序
							} catch (Exception ex) {
								if (ex.getMessage().equals("-152")) {
									String realSN = iClient.getSN4Label(iSN);// 如果条码未识别，先查询看看是否为标签
									if (realSN != null) {
										iSN = realSN;
										iClient.uploadState(iSN, bState, bRepair, icallXRay,procedure);
									} else {
										throw ex;
									}
								} else {
									throw ex;
								}
							}
						} else {// 判断是否加严清单中的案例
							m_panelSonResultState[] sonsState = new m_panelSonResultState[sons.size()];
							boolean gResult = true;

							for (int jj = 0; jj < sonsState.length; jj++) {
								if (((Element) (sons.get(jj))).getName().equals("subPanel")) {
									String iPos = ((Element) (sons.get(jj))).attribute("pos").getValue();
									String iResult = ((Element) (sons.get(jj))).attribute("state").getValue();// 0=无异常
									List<?> errComps = ((Element) sons.get(jj)).elements();
									
									sonsState[jj] = new m_panelSonResultState();//
									/**
									 * 	有复判的位号
									 */
									Vector<String> iRpComps =getRepairComps(errComps);//ss
									if (iRpComps.size() > 0) {// 判断是否加严清单中的案例
										Vector<String> iXRComps=getSPIxRayListComps(iPos);
										if (iXRComps.size() > 0) {
											for (int m = 0; m < iRpComps.size(); m++) {// 验证是否在加严清单中
												String strr = iRpComps.get(m);
												if (strr != null && strr.length() > 0) {
													if (iXRComps.indexOf(iRpComps.get(m)) >= 0) {
														icallXRay = true;
														sonsState[jj].callXRay = true;
														if(sonsState[jj].xRayComps.length()>0) {
															sonsState[jj].xRayComps=sonsState[jj].xRayComps+",";
														}
														sonsState[jj].xRayComps=sonsState[jj].xRayComps+iRpComps.get(m);
//														m = iRpComps.size() + 1;// 改成每一个加严清单中的位号都要可查询
													}
												}
											}
										}
									}
									sonsState[jj].pos = Integer.parseInt(iPos);
									sonsState[jj].isPass = false;
									if (iResult != null && iResult.equals("0")) {
										sonsState[jj].isPass = true;
										try {
											String iiRepair = ((Element) (sons.get(jj))).element("errComps")
													.attribute("comps").getValue();
											if (iiRepair != null && iiRepair.length() > 0) {
												sonsState[jj].isRepair = true;
											}
										} catch (Exception a) {
											;
										}
									} else {
										gResult = false;
										sonsState[jj]=getErrCompsInfo(sonsState[jj],errComps);
									}
									if(errComps!=null && errComps.size()>0) {
										iClient.uploadSubPanelResultData(iSN,iPos,errComps);
									}// 自动填充555
								} else {
									sonsState[jj] = new m_panelSonResultState();
								}
								
							}
							if(sons.size()==0) {
								iClient.uploadPanelState(iSN, gResult, bRepair, icallXRay,procedure);
							}else {
								
								if (procedure.equals("A0020") 
//										|| sCl.myProcedureNumber.equals("A0030")
										|| procedure.equals("A0040")) {
									iClient.uploadPanelState(iSN, gResult, bRepair, icallXRay,procedure);
								} else {
//									if(!gResult) {
//										AKSLogs(230810, "" +gXML);
//									}
									iClient.uploadPanelState(iSN, sonsState,procedure);
								}
							}
							
						}
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5501, "" + iSN + "_" + iStation + "_" + iLayer);
					} catch (Exception e) {
						String str = errCodeManagement.getErrText(e.getMessage());
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", str);
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5500, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
					}
				}
			}else if (msgClass.equals("552")) {//
				Element eleCheckList = ((Element) Mroot.clone()).element("body");

				Mroot.remove(Mroot.element("body"));
				Mroot.addElement("body");
//				if (iClient.myProcedureNumber.equals("A0200")) {//  后续要弄成标准化的可配置项
//					
//				}else {
//					iClient.orderActiveRequired = true;
//				}
				Vector<Thread> uplTs=new Vector<Thread>();
				List<?> bodySons = eleCheckList.elements();//panel字段
				if (bodySons.size() > 0) {
					int listLen = bodySons.size();
					for (int listI = 0; listI < listLen; listI++) {
						Element eleR = Mroot.element("body").addElement("result");
						String iRepair = "0";
						iSN=((Element) (bodySons.get(listI))).attribute("pcbID").getValue();
						iLayer=((Element) (bodySons.get(listI))).attribute("productSide").getValue();
						iState=((Element) (bodySons.get(listI))).attribute("state").getValue();
						try {
							iRepair = ((Element) (bodySons.get(listI))).attribute("repairType").getValue();
						} catch (Exception e) {
							iRepair = "0";
						}
						Thread uplT=uploadPCBs(iStation,iSN,iLayer,iState,iRepair,transactionID,eleR);
						uplTs.add(uplT);
					}
					for (int listI = 0; listI < listLen; listI++) {
						uplTs.get(listI).join();
					}
					AKSLogs(5521, "" + transactionID + "_" + iStation );
				}
				
			} else if (msgClass.equals("555")) {// 上传测量数据 
				iSN = Mroot.element("body").element("panel").attribute("pcbID").getValue();
				iLayer = Mroot.element("body").element("panel").attribute("productSide").getValue();
				iState = Mroot.element("body").element("panel").attribute("state").getValue();
				Element eleCheckList = Mroot.element("body").element("checkList");
				List<?> checkListSons = eleCheckList.elements();
				iiLayer = Integer.parseInt(iLayer);
				iiState = Integer.parseInt(iState);
				Mroot.element("body").remove(Mroot.element("body").element("panel"));
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				int listI=0;
				try {
					if (checkListSons.size() > 0) {
						int listLen = checkListSons.size();
						m_resultData[] iData = new m_resultData[listLen];
						for (listI = 0; listI < listLen; listI++) {
							if(listI==422) {
								System.out.print("testCounter");
							}
							iData[listI] = new m_resultData();
							iData[listI].pos=((Element) (checkListSons.get(listI))).attribute("pos").getValue();
							iData[listI].name = ((Element) (checkListSons.get(listI))).attribute("name").getValue();
							iData[listI].unit = ((Element) (checkListSons.get(listI))).attribute("unit").getValue();
							iData[listI].max = ((Element) (checkListSons.get(listI))).attribute("upperLimit")
									.getValue();
							iData[listI].min = ((Element) (checkListSons.get(listI))).attribute("lowerLimit")
									.getValue();
							iData[listI].value = ((Element) (checkListSons.get(listI))).attribute("value").getValue();
							iData[listI].result = ((Element) (checkListSons.get(listI))).attribute("state").getValue();
							try {
								iData[listI].equipment = ((Element) (checkListSons.get(listI))).attribute("equipment").getValue();
							}catch(Exception e) {
								iData[listI].equipment = "";
							}
						}
						iClient.uploadData(iSN, iData);
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(5551, "" + iSN + "_" + iStation + "_" + iLayer);
					}else {
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "测量项目0条");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(5551, "" + iSN + "_" + iStation + "_" + iLayer+"_"+"测量项目0条");
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5550, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
			}else if(msgClass.equals("556")){//上传测量数据并过站
				iSN=Mroot.element("body").element("panel").attribute("pcbID").getValue();
				iLayer=Mroot.element("body").element("panel").attribute("productSide").getValue();
				iState=Mroot.element("body").element("panel").attribute("state").getValue();
				Element eleCheckList=Mroot.element("body").element("checkList");
				List<?> checkListSons=eleCheckList.elements();
				iiLayer=Integer.parseInt(iLayer);
				iiState=Integer.parseInt(iState);
				Mroot.element("body").remove(Mroot.element("body").element("panel"));
				Element eleR=Mroot.element("body").addElement("result");
				try {
					if(checkListSons.size()>0) {
						int listLen=checkListSons.size();
						m_resultData [] iData=new m_resultData [listLen];
						for(int listI=0;listI<listLen;listI++) {
							iData[listI]=new m_resultData();
							iData[listI].pos=((Element) (checkListSons.get(listI))).attribute("pos").getValue();
							iData[listI].name=((Element)(checkListSons.get(listI))).attribute("name").getValue();
							iData[listI].unit=((Element)(checkListSons.get(listI))).attribute("unit").getValue();
							iData[listI].max=((Element)(checkListSons.get(listI))).attribute("upperLimit").getValue();
							iData[listI].min=((Element)(checkListSons.get(listI))).attribute("lowerLimit").getValue();
							iData[listI].value=((Element)(checkListSons.get(listI))).attribute("value").getValue();
							iData[listI].result=((Element)(checkListSons.get(listI))).attribute("state").getValue();
						}
						iClient.uploadData(iSN, iData);
						if(iState.equals("0")) {//556比555多个过站state，拼板的话只能整拼板一起
							iClient.uploadState(iSN, true,"-1");
						}else {
							iClient.uploadState(iSN, false,"-1");
						}
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5561,""+iSN+"_"+iStation+"_"+iLayer);
					}
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5560,""+iSN+"_"+iStation+"_"+iLayer+"_"+str);
				}
			}else if(msgClass.equals("560")){//整托盘点胶过站，记录胶水静止时间为测量值,并记录托盘产品关系
				Element elePallet=Mroot.element("body").element("pallet");
				String palletID=elePallet.attribute("ID").getValue();
				String stationID=Mroot.element("body").element("station").attribute("ID").getValue();
				stationClient coatingStation=EGS.sSheets.getStationClient(stationID);//获取点胶的工位
				List<?> elePalletSons=elePallet.elements();
				String [] iSNs= null;
				String [] cost= null;
				
				int lenth=elePalletSons.size();
				if(lenth>0) {
					iSNs= new String[lenth];
					cost= new String[lenth];
					for(int i=0;i<lenth;i++) {
						iSNs[i]=((Element)(elePalletSons.get(i))).attribute("sn").getValue();
						cost[i]=((Element)(elePalletSons.get(i))).attribute("seconds").getValue();
					}
				}
				Mroot.element("body").remove(Mroot.element("body").element("pallet"));
				Element eleR=Mroot.element("body").addElement("result");
				try {
					if(lenth>0) {
						String palletInfo="";
						String palletInfo1="";//用于记log
						for(int listI=0;listI<lenth;listI++) {
							m_resultData [] iData=new m_resultData[1];
							iData[0]=new m_resultData();
							iData[0].pos="-1";
							iData[0].name="胶水静置时间";
							iData[0].unit="s";
							iData[0].max="1";
							iData[0].min="0";
							iData[0].value=cost[listI];
							iData[0].result="Pass";
							coatingStation.uploadData(iSNs[listI], iData);
							coatingStation.uploadState(iSNs[listI], true,"-1");
							if(listI>0) {
								palletInfo=palletInfo+",";
								palletInfo1=palletInfo1+",";
							}
							palletInfo=palletInfo+"('"+palletID+"','"+iSNs[listI]+"')";
							palletInfo1=palletInfo1+iSNs[listI];
						}
						if(palletInfo.length()>0) {
							sql.Command("delete from [dbo].[resource_pallet] where palletID = '"+palletID+"'");
							sql.Command("insert [dbo].[resource_pallet] (palletID,serialNumber) "
								+ "values "+palletInfo);
						}
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5601,""+iStation+"_"+palletID+"_"+palletInfo1);
					}
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5600,""+iStation+"_"+palletID+"_"+str);
				}
			}else if(msgClass.equals("561")){//绑定托盘
				Element elePallet=Mroot.element("body").element("pallet");
				String palletID=elePallet.attribute("ID").getValue();
				List<?> elePalletSons=elePallet.elements();
				String [] iSNs= null;
				
				int lenth=elePalletSons.size();
				if(lenth>0) {
					iSNs= new String[lenth];
					for(int i=0;i<lenth;i++) {
						iSNs[i]=((Element)(elePalletSons.get(i))).attribute("sn").getValue();
					}
				}
				Mroot.element("body").remove(Mroot.element("body").element("pallet"));
				Element eleR=Mroot.element("body").addElement("result");
				try {
					if(lenth>0) {
						String palletInfo="";
						String palletInfo1="";//用于记log
						for(int listI=0;listI<lenth;listI++) {
							if(listI>0) {
								palletInfo=palletInfo+",";
								palletInfo1=palletInfo1+",";
							}
							palletInfo=palletInfo+"('"+palletID+"','"+iSNs[listI]+"')";
							palletInfo1=palletInfo1+iSNs[listI];
						}
						if(palletInfo.length()>0) {
							sql.Command("delete from [dbo].[resource_pallet] where palletID = '"+palletID+"'");
							sql.Command("insert [dbo].[resource_pallet] (palletID,serialNumber) "
								+ "values "+palletInfo);
						}
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5611,""+iStation+"_"+palletID+"_"+palletInfo1);
					}
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5610,""+iStation+"_"+palletID+"_"+str);
				}
			}else if(msgClass.equals("572")){//读取托盘信息
				Element elePallet=Mroot.element("body").element("pallet");
				String palletID=elePallet.attribute("ID").getValue();
				List<?> elePalletSons=elePallet.elements();
				Element eleB=Mroot.element("body");
				Element eleR=eleB.addElement("result");
				eleB.remove(elePallet);
				eleB.add(elePallet);
				
				try {
					JTable t=sql.select("select serialNumber from [dbo].[resource_pallet] where palletID='"
							+palletID+"' ");
					if(t!=null && t.getRowCount()>0) {
						for(int i=0;i<t.getRowCount();i++) {
							elePallet.addElement("pcb").addAttribute("sn", ""+t.getValueAt(i, 0));
						}
					}
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5721,""+iStation+"_"+palletID);
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5720,""+iStation+"_"+palletID+"_"+str);
				}
			}else if(msgClass.equals("571")){//清空托盘信息
				Element elePallet=Mroot.element("body").element("pallet");
				String palletID=elePallet.attribute("ID").getValue();
				List<?> elePalletSons=elePallet.elements();
				Element eleB=Mroot.element("body");
				Element eleR=eleB.addElement("result");
				
				try {
					sql.Command("delete from [dbo].[resource_pallet] where palletID='"+palletID+"' ");
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5711,""+iStation+"_"+palletID);
				} catch (Exception e) {
					String str=errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(5710,""+iStation+"_"+palletID+"_"+str);
				}
			} else if (msgClass.equals("5506")) {// add by Akite 2020-04-30 edit by Akite 2020-11-17
				iSN = Mroot.element("body").element("panelcomp").element("main").element("panel").attribute("pcbID")
						.getValue();
				iLayer = Mroot.element("body").element("panelcomp").element("main").element("panel")
						.attribute("productSide").getValue();
				iState = Mroot.element("body").element("panelcomp").element("main").element("panel").attribute("state")
						.getValue();
				iiLayer = Integer.parseInt(iLayer);
				iiState = Integer.parseInt(iState);

				boolean bState = false;
				if (iiState == 0) {
					bState = true;
				}
//				System.out.println(Mroot.asXML());
				Element eleBody = Mroot.element("body").createCopy();
				Mroot.element("body").remove(Mroot.element("body").element("panelcomp"));
				Element eleR = Mroot.element("body").addElement("resultcomp");
				Element eleM = eleR.addElement("main");
				Element eleS = eleR.addElement("sub");
				Element eleL = eleR.addElement("label");
				boolean isget=false;
				try {
					
					if (iiLayer != iClient.myLayer) {
						String str = "工单激活面别与产品上传面别不一致！";
						eleM.addAttribute("errorCode", "1");
						eleM.addAttribute("errorText", str);
						eleM.addAttribute("actionCode", transactionID);
						AKSLogs(55060, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
						return MSG;
					}
					isget=true;
				}catch(Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(55060, "" + iSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
				if(isget) {
					try {
						String iLable = eleBody.element("panelcomp").element("label").element("panel").attribute("pcbID")
								.getValue();
						Element eleLR = eleL.addElement("result");
						eleLR.addAttribute("barcode", iLable);
						try {
							iClient.uploadState_label(iSN, bState, iLable);
							String errorCode, errorText, actionCode;
							errorCode = "0";
							errorText = "Valid";
							actionCode = "0";
							eleLR.addAttribute("errorCode", errorCode);
							eleLR.addAttribute("errorText", errorText);
							eleLR.addAttribute("actionCode", actionCode);
						} catch (Exception e) {
							String str = errCodeManagement.getErrText(e.getMessage());
							eleLR.addAttribute("errorCode", "1");
							eleLR.addAttribute("errorText", str);
							eleLR.addAttribute("actionCode", transactionID);
						}
					} catch (Exception e3) {
						;
					}
					try {
						Element eleSub = eleBody.element("panelcomp").element("sub");
						List<?> sons = eleSub.elements();
						boolean assemblyNG = false;
						String NGsons = "";
						if (sons.size() > 0) {
							String[] subParts = new String[sons.size()];
							String[] aState = new String[sons.size()];
							for (int n = 0; n < sons.size(); n++) {
								Element eleSR = eleS.addElement("result");
								subParts[n] = ((Element) sons.get(n)).attribute("pcbID").getValue();
								aState[n] = ((Element) sons.get(n)).attribute("state").getValue();
								eleSR.addAttribute("barcode", subParts[n]);
								try {
									iClient.assembling(iSN, subParts[n], aState[n]);
									eleSR.addAttribute("errorCode", "0");
									eleSR.addAttribute("errorText", "Valid");
									eleSR.addAttribute("actionCode", "0");
								} catch (Exception e2) {
									JTable t = sql.select("select * from assembled where masterSN='" + iSN
											+ "' and partSN='" + subParts[n] + "' ");
									if (t == null || t.getRowCount() < 1) {
										assemblyNG = true;
										if (NGsons.length() > 0) {
											NGsons = NGsons + ",";
										}
										NGsons = NGsons + subParts[n];
										String str = errCodeManagement.getErrText(e2.getMessage());
										eleSR.addAttribute("errorCode", "1");
										eleSR.addAttribute("errorText", str);
										eleSR.addAttribute("actionCode", aState[n]);
									} else {
										eleSR.addAttribute("errorCode", "0");
										eleSR.addAttribute("errorText", "Valid");
										eleSR.addAttribute("actionCode", "0");
									}
	
								}
							}
							String iiSN = iSN;
							boolean isFail = assemblyNG;
							new Thread(new Runnable() {
								String gSN = iiSN;
								boolean gIsFail = isFail;
//								stationClient uSC=iClient.copyStation(null);
								stationClient uSC=iClient;
								@Override
								public void run() {
									try {
										try {
											uSC.assembling(gSN, gSN, "0");
										} catch (Exception e3) {
											String str = errCodeManagement.getErrText(e3.getMessage());
											System.out.println(str);
										}
										if (!gIsFail) {
											uSC.uploadState(gSN, true,"-1");
										}
									} catch (Exception e2) {
										String str = errCodeManagement.getErrText(e2.getMessage());
										System.out.println(str);
									}
//									try {
//										uSC.close();
//									} catch (Exception e) {
//										;
//									}
								}
							}).start();
							String iErrCode = "0";
							String iErrText = "Valid";
							int iType = 55061;
							if (assemblyNG) {
								iErrCode = "1";
								iErrText = NGsons + "_装配失败，已有装配历史未拆分。";
								iType = 55060;
							}
							eleM.addAttribute("errorCode", iErrCode);
							eleM.addAttribute("errorText", iErrText);
							eleM.addAttribute("actionCode", "0");
							AKSLogs(iType, "" + iSN + "_" + iStation + "_" + iLayer);
							return MSG;
						}
					} catch (Exception e3) {
						;
					}
					eleM.addAttribute("errorCode", "0");
					eleM.addAttribute("errorText", "Valid");
					eleM.addAttribute("actionCode", "0");
					AKSLogs(55061, "" + iSN + "_" + iStation + "_" + iLayer);
				}

			} else if (msgClass.equals("570")) {// 整台车上，整台车下
				String iType = Mroot.element("body").element("operation").attribute("type").getValue();
				String iCartID = Mroot.element("body").element("operation").attribute("cartID").getValue();
//				String iCartSerial=Mroot.element("body").element("operation").attribute("cartSerial").getValue();
//				String iMachineTable=Mroot.element("body").element("operation").attribute("machineTable").getValue();
				String iTimestamp = Mroot.element("body").element("operation").attribute("timestamp").getValue();
				String iOperator = Mroot.element("body").element("operation").attribute("operator").getValue();

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					String strType = "卸载";
					if (iType.equals("0")) {
						String sqlstr = "delete from setupLocation where station = '" + iCartID + "'";
						sql.Command(sqlstr);
						sqlstr = "insert into setupLocation (station,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity) "
								+ "(select cartID,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity from setupLocation "
								+ "where cartID='" + iCartID + "' and station='" + iStation + "')";
						sql.Command(sqlstr);
						sqlstr = "delete from setupLocation where station = '" + iStation + "' and cartID='" + iCartID
								+ "'";
						sql.Command(sqlstr);

						sqlstr = "update materialID set location='cartID=" + iCartID + "' where materialID in "
								+ "(select reel from setupLocation " + "where cartID='" + iCartID + "') or preReel in "
								+ "(select reel from setupLocation where cartID='" + iCartID + "')";
						sql.Command(sqlstr);
					} else {
						String sqlstr = "delete from setupLocation where station = '" + iStation + "'";
						sql.Command(sqlstr);
						sqlstr = "insert into setupLocation (station,cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity) "
								+ "(select '" + iStation + "',cartID,location,p1,p2,p3,feeder,reel,nextReel,quantity "
								+ "from setupLocation where cartID='" + iCartID + "' )";
						sql.Command(sqlstr);
						sqlstr = "delete from setupLocation where station != '" + iStation + "' and cartID='" + iCartID
								+ "'";
						sql.Command(sqlstr);

						sqlstr = "update materialID set location='" + iCartID + "@" + iStation
								+ "' where materialID in " + "(select reel from setupLocation " + "where cartID='"
								+ iCartID + "') or preReel in " + "(select reel from setupLocation where cartID='"
								+ iCartID + "')";
						sql.Command(sqlstr);
						strType = "安装";
					}
					String oOrder=getStationOrder(iStation);
					sql.Command(
							"insert into materialLog (state,operator,machineTime,materialID,quantity,location,feeder,tips,station,orderNumber) "
									+ "(select " + iType + ",'" + iOperator + "','" + iTimestamp
									+ "',reel,quantity,location,feeder,'cartID=" + iCartID + strType + "','"+iStation+"','"+oOrder+"' "
									+ "from setupLocation where cartID='" + iCartID + "')");

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5701, "" + iStation + "_" + iCartID + "_" + iType);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5700, "" + iStation + "_" + iCartID + "_" + iType);
				}
			} else if (msgClass.equals("580")) {// 试运行阶段二，用580的时机来切换工单到对应工位。
				String lv2Key = "orderInfo";
				String orderNo = null;
				try {
					orderNo = Mroot.element("body").element(lv2Key).attribute("order_no").getValue();// iSN=modelNo
				} catch (Exception e) {
					lv2Key = "modelInfo";
					;
				}
				if (orderNo == null) {
					orderNo = "";
				}
				iLayer = Mroot.element("body").element(lv2Key).attribute("side").getValue();
				iiLayer = Integer.parseInt(iLayer);
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");

				String str = "SELECT a.* FROM vOrderList_new a  \r\n" + 
						"LEFT JOIN product_struct b ON b.masterNumber=a.partNumber   \r\n" + 
						"where (a.orderNumber='"+orderNo+"')   and b.isMarked=1 \r\n" + 
						"UNION\r\n" + 
						"SELECT a.* FROM vOrderList_new a  \r\n" + 
						"LEFT JOIN product_struct b ON b.masterNumber=a.partNumber   \r\n" + 
						"where (a.panelOrderNumber='"+orderNo+"')   and b.isMarked=1 ";
				JTable t = sql.select(str);
				int isMarked = 0;
				if (t != null && t.getRowCount() > 0) {
					isMarked = 1;
				}

				if(orderNo==null || orderNo.length()<1) {
					str = "工单号获取失败，请检查激活软件版本";
					eleR.addAttribute("isMarked", "" + isMarked);
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5800, "" + orderNo + "_" +iStation + ":_" + str);
				}else {
					try {
						if (iClient.myProcedureNumber.equals("A0010")) {// 检查条码是否发行到指定数量（计划+追加）
							if (orderNo != null && orderNo.length() > 0) {
								new Thread(new Runnable() {// 每个工单都要按照非必过站设置SN初始状态
									String oldOrder= iClient.activeOrderNumber;
									@Override
									public void run() {
										
										try {
											laserUPHreporter lr=new laserUPHreporter(sql);
											lr.updateReport(oldOrder);
										} catch (Exception e) {
											;
										}
									}
								}).start();
							}
						}
						c_order co=new c_order(sql);
    					co.checkOrderState(orderNo);
						
						iClient.changeStationOrder(orderNo, iiLayer, 0);
						if (iClient.myProcedureNumber.equals("A0010")) {// 检查条码是否发行到指定数量（计划+追加）
							if (orderNo != null && orderNo.length() > 0) {
								// 根据工单获取相关刻码规则信息，检查刻码规则是否已审核
		    					co.checkSNtoolReleaseFlag(orderNo, "A0010");// 抛出刻码规则未审核的异常 
								String gOrderNumber = orderNo;
								new Thread(new Runnable() {// 每个工单都要按照非必过站设置SN初始状态
									String mgOrderNumber = gOrderNumber;
									@Override
									public void run() {
										try {
											orderManagement om = new orderManagement(sql);
											om.createSN4order(mgOrderNumber);
										} catch (Exception e) {
											;
										}
									}
								}).start();
								
								sql.Command("delete from [QM].[temp10000_laserInspec] "
										+ "where stationID='"+iStation+"' ");
								JTable maxCounterT=sql.select("select olp.panelNumber,isnull(pli.activeQty,5) "
										+ "from [dbo].[order_list_panel] olp \r\n" + 
										"LEFT JOIN [QM].[vPanelLaserInspec] pli ON pli.panelNumber=olp.panelNumber \r\n" + 
										"where panelOrderNumber='"+orderNo+"' ");
								String maxCounter=""+maxCounterT.getValueAt(0, 1);
								// 激活成功，重置计数器和计时器  
								String strSQL1="update [dbo].[assetStation] "
										+ "set counter=0,maxCounter="+maxCounter+",lastCheck=null,EMflag=0,QCflag=0 "
										+ "where ID='"+iStation+"' ";
								sql.Command(strSQL1);
							}
						}else if (iClient.myProcedureNumber.equals("A0040")) {//TODO 230703配合香振炉温板软件 
							String iLine=iStation.replaceAll("S01-L", "").split("-")[0];
							JTable t1=sql.select("select panelNumber from [dbo].[order_list_panel] where panelOrderNumber='"
									+orderNo+"' ");
							String iPanel=""+t1.getValueAt(0, 0);
							String strSQL1="update [sunlight].[FurnaceKanbanInstructs] set WorkOrder='"
									+orderNo+"',MakeUpOrder='"+iPanel+"' where line="+Integer.parseInt(iLine);
							sql.Command(strSQL1);
						}
						new Thread(new Runnable() {
							
							@Override
							public void run() {
								try {
									iClient.freshOrderWipQty_station(iStation);
								} catch (Exception e1) {
									;
								}
							}
						}).start();
						eleR.addAttribute("isMarked", "" + isMarked);
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(5801, "" + orderNo + "_" + iStation + "_" + iLayer);
					} catch (Exception e) {
						try {
							String str1=e.getMessage();
							if(str1.split("@")[0].equals("-184")) {
								throw new Exception(""+str1.split("@")[1]+"的刻码规则未审核");
							}
							throw e;
						} catch (Exception e1) {
							str = errCodeManagement.getErrText(e1.getMessage());
							eleR.addAttribute("isMarked", "" + isMarked);
							eleR.addAttribute("errorCode", "1");
							eleR.addAttribute("errorText", str);
							eleR.addAttribute("actionCode", "0");
							AKSLogs(5800, "" + orderNo + "_" +iStation + ":_" + str);
						}
					}
				}
				
			} else if (msgClass.equals("590")) {// 物料状态变更（卸载和耗尽）
				String str = Mroot.element("body").element("operation").attribute("type").getValue();
				if (str.length() < 1) {
					str = "1";
				}

				/**
				 * 2=卸载物料 3=物料数量清零
				 */
				String isUnmount = str;
				String op = Mroot.element("body").element("operation").attribute("operator").getValue();
				String time = Mroot.element("body").element("operation").attribute("timestamp").getValue();
				String iMaterialID = Mroot.element("body").element("operation").attribute("materialID").getValue();
				String tips = Mroot.element("body").element("operation").attribute("reason").getValue();
				String cartID = "";
				try {
					cartID = Mroot.element("body").element("operation").attribute("cartID").getValue();
				} catch (Exception e3) {
					;
				}
				if (cartID == null || cartID.length() < 1) {
					cartID = "NA";
				}
				String quantity = "-1";
				try {
					quantity = Mroot.element("body").element("material").attribute("qty").getValue();
				} catch (Exception e3) {
					;
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");

				c_material cm = new c_material(sql);
				m_material iReel = cm.getMaterial(iMaterialID);
				try {
					if(iReel.location!=null && iReel.location.equals("WMS")) {//TODO materialLOG 记录卸载
						cm.logMaterial(0, op, time, iMaterialID, Double.parseDouble(quantity), iStation,
								cartID + "@NA" , "NA", tips+"@该物料已退库未领料","NA");
						throw new Exception("-185");//该物料已退库未领料
					}
					JTable t = sql.select("select location,feeder from materialLog where " + "materialID='"
							+ iMaterialID + "' and state>0 " + "order by createTime desc");// 查找被扫描的那盘料
					if (t == null || t.getRowCount() < 1) {
						throw new Exception("-180");// 查不到报错
					}
					String location1 = (String) t.getValueAt(0, 0);
					String[] locator = location1.split("@");
					if (locator.length > 1) {
						location1 = locator[1];
					}
					String location = location1;
					String feeder = (String) t.getValueAt(0, 1);
					String iqty = quantity;
					String icartID = cartID;
					Thread iTH = new Thread(new Runnable() {// 查到了准备卸载
						int istate = Integer.parseInt(isUnmount);
						String iOP = op;
						String iTime = time;
						String oMaterial = iMaterialID;
						String iiStation = iStation;
						String iicartID = icartID;
						String iLocation = location;
						String iFeeder = feeder;
						String iTips = tips;
						String iiqty = iqty;
						m_material sReel = iReel;

						@Override
						public void run() {
							try {
								String mlocation = iiStation + "@" + iicartID + "@" + iLocation;
								double sqty = Double.parseDouble(iiqty);
								c_material cmt = new c_material(sql);
								try {
									String oOrder=getStationOrder(iiStation);
									cmt.logMaterial(0, iOP, iTime, oMaterial, sqty, iiStation,
											iicartID + "@" + iLocation, iFeeder, iTips,oOrder);
									if (istate == 2) {// 卸载
										String str = "update setupLocation set cartID='',feeder='',reel='',"
												+ "nextReel='',lastEdit=GETDATE() where reel='" + oMaterial + "'";
										sql.Command(str);
										str = "update materialID set location='lineBIN',quantity=" + iiqty
												+ " where materialID='" + oMaterial + "'";
										sql.Command(str);
										m_material nReel = null;
										if (sReel.nextReel != null && sReel.nextReel.length() > 0) {// 末端所有接料都要改成LineBIN
											str = "update materialID set location='lineBIN' where materialID='"
													+ sReel.nextReel + "'";
											sql.Command(str);
											nReel = cm.getMaterial(sReel.nextReel);
											cmt.logMaterial(0, iOP, iTime, nReel.materialID, nReel.quantity, iiStation,
													iicartID + "@" + iLocation, iFeeder, iTips+"["+nReel.preReel+"的接料]",oOrder);
											boolean b = (nReel.nextReel != null && nReel.nextReel.length() > 0);
											int flag=0;
											while (b && flag <5) {
												str = "update materialID set location='lineBIN' where materialID='"
														+ nReel.nextReel + "'";
												sql.Command(str);
												nReel = cm.getMaterial(nReel.nextReel);
												cmt.logMaterial(0, iOP, iTime, nReel.materialID, nReel.quantity, iiStation,
														iicartID + "@" + iLocation, iFeeder, iTips+"["+nReel.preReel+"的接料]",oOrder);
												b = (nReel.nextReel != null && nReel.nextReel.length() > 0);
												flag+=1;
											}
										}
									} else if (istate == 3) {// 物料用光
										m_material nReel = null;
										if (sReel.nextReel != null && sReel.nextReel.length() > 0) {
											nReel = cm.getMaterial(sReel.nextReel);
										}
										String str = "update materialID set quantity=0,location='lineBIN' where materialID='"
												+ sReel.materialID + "'";
										sql.Command(str);
										if (nReel != null) {
											str = "update setupLocation set reel='" + nReel.materialID + "',nextReel='"
													+ nReel.nextReel + "',quantity=" + nReel.quantity
													+ ",lastEdit=GETDATE() where reel='" + sReel.materialID + "'";
											sql.Command(str);
											str = "update materialID set location='" + mlocation
													+ "',preReel='' where materialID='" + nReel.materialID + "'";
											sql.Command(str);
											cmt.logMaterial(1, iOP, iTime, nReel.materialID, nReel.quantity, iiStation,
													iicartID + "@" + iLocation, iFeeder, "接料启用",oOrder);
										} else {
											str = "update setupLocation set cartID='',reel='',nextReel='',quantity=0,lastEdit=GETDATE() where reel='"
													+ sReel.materialID + "'";
											sql.Command(str);
										}
									}
								} catch (Exception e3) {
//									String str=errCodeManagement.getErrText(e3.getMessage());
									;
								}
							} catch (Exception e2) {
//								String str=errCodeManagement.getErrText(e2.getMessage());
								;
							}
//							try {
//								iisql.close();
//							}catch(Exception e1) {
//								;
//							}
						}
					});
					iTH.start();

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5901, cartID + "_" + iStation + "_" + iMaterialID);
//					return null;
				} catch (Exception e) {
					str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5900, cartID + "_" + iStation + "_" + iMaterialID + "_" + str);
				}
			} else if (msgClass.equals("591")) {// 物料状态变更（卸载和耗尽）
				String str =null;
				try {
					str = Mroot.element("body").element("operation").attribute("type").getValue();
				}catch(Exception ee) {
					str = "1";
				}
				if (str.length() < 1) {
					str = "1";
				}

				/**
				 * 2=卸载物料 3=物料数量清零
				 */
				String isUnmount = str;
				String op = Mroot.element("body").element("operation").attribute("operator").getValue();
				String time = Mroot.element("body").element("operation").attribute("timestamp").getValue();
				String iMaterialID = Mroot.element("body").element("operation").attribute("materialID").getValue();
				String tips = Mroot.element("body").element("operation").attribute("reason").getValue();
				String cartID = "";
				try {
					cartID = Mroot.element("body").element("operation").attribute("cartID").getValue();
				} catch (Exception e3) {
					;
				}
				if (cartID == null || cartID.length() < 1) {
					cartID = "NA";
				}
				String quantity = "-1";
				try {
					quantity = Mroot.element("body").element("material").attribute("qty").getValue();
				} catch (Exception e3) {
					;
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");

				c_material cm = new c_material(sql);
				m_material iReel = cm.getMaterial(iMaterialID);

				try {

					if(iReel.location!=null && iReel.location.equals("WMS")) {
						throw new Exception("-185");//该物料已退库未领料
					}
					JTable t = sql.select("select location,feeder from setupLocation where " + "reel='" + iMaterialID + "'");// 查找被扫描的那盘料
					if (t == null || t.getRowCount() < 1) {
						throw new Exception("-180");// 查不到报错
					}
					String location = (String) t.getValueAt(0, 0);
					String feeder = (String) t.getValueAt(0, 1);
					String iqty = quantity;
					String icartID = cartID;
					Thread iTH = new Thread(new Runnable() {// 查到了准备卸载
						int istate = Integer.parseInt(isUnmount);
						String iOP = op;
						String iTime = time;
						String oMaterial = iMaterialID;
						String iiStation = iStation;
						String iicartID = icartID;
						String iLocation = location;
						String iFeeder = feeder;
						String iTips = tips;
						String iiqty = iqty;
						m_material sReel = iReel;

						@Override
						public void run() {
							try {
								String mlocation = iiStation + "@" + iicartID + "@" + iLocation;
								double sqty = Double.parseDouble(iiqty);
								c_material cmt = new c_material(sql);
								try {
									String oOrder=getStationOrder(iiStation);
									cmt.logMaterial(0, iOP, iTime, oMaterial, sqty, iiStation,
											iicartID + "@" + iLocation, iFeeder, iTips,oOrder);
									if (istate == 2) {// 卸载
										String str = "update setupLocation set cartID='',feeder='',reel='',"
												+ "nextReel='',lastEdit=GETDATE() where reel='" + oMaterial + "'";
										sql.Command(str);
										str = "update materialID set location='lineBIN',quantity=" + iiqty
												+ " where materialID='" + oMaterial + "'";
										sql.Command(str);
										m_material nReel = null;
										if (sReel.nextReel != null && sReel.nextReel.length() > 0) {// 末端所有接料都要改成LineBIN
											str = "update materialID set location='lineBIN' where materialID='"
													+ sReel.nextReel + "'";
											sql.Command(str);
											nReel = cm.getMaterial(sReel.nextReel);
											boolean b = (nReel.nextReel != null && nReel.nextReel.length() > 0);
											int flag=0;
											while (b && flag<5) {
												str = "update materialID set location='lineBIN' where materialID='"
														+ nReel.nextReel + "'";
												sql.Command(str);
												nReel = cm.getMaterial(nReel.nextReel);
												b = (nReel.nextReel != null && nReel.nextReel.length() > 0);
												flag+=1;
											}
										}
									} else if (istate == 3) {// 物料用光
										m_material nReel = null;
										if (sReel.nextReel != null && sReel.nextReel.length() > 0) {
											nReel = cm.getMaterial(sReel.nextReel);
										}
										String str = "update materialID set quantity=0,location='lineBIN' where materialID='"
												+ sReel.materialID + "'";
										sql.Command(str);
										if (nReel != null) {
											str = "update setupLocation set reel='" + nReel.materialID + "',nextReel='"
													+ nReel.nextReel + "',quantity=" + nReel.quantity
													+ ",lastEdit=GETDATE() where reel='" + sReel.materialID + "'";
											sql.Command(str);
											str = "update materialID set location='" + mlocation
													+ "',preReel='' where materialID='" + nReel.materialID + "'";
											sql.Command(str);
										} else {
											str = "update setupLocation set cartID='',reel='',nextReel='',quantity=0,lastEdit=GETDATE() where reel='"
													+ sReel.materialID + "'";
											sql.Command(str);
										}
									}
								} catch (Exception e3) {
//									String str=errCodeManagement.getErrText(e3.getMessage());
									;
								}
							} catch (Exception e2) {
//								String str=errCodeManagement.getErrText(e2.getMessage());
								;
							}
//							try {
//								iisql.close();
//							}catch(Exception e1) {
//								;
//							}
						}
					});
					iTH.start();

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5911, cartID + "_" + iStation + "_" + iMaterialID);
//					return null;
				} catch (Exception e) {
					str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5910, cartID + "_" + iStation + "_" + iMaterialID + "_" + str);
				}
			}else if (msgClass.equals("595")) {// 物料数量同步
				String str ="";
				String iMaterialID = Mroot.element("body").element("material").attribute("id").getValue();
				String quantity = "-1";
				try {
					quantity = Mroot.element("body").element("material").attribute("qty").getValue();
				} catch (Exception e3) {
					;
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");

				c_material cm = new c_material(sql);
				m_material iReel = cm.getMaterial(iMaterialID);
				try {
					str = "update materialID set location='lineBIN',quantity=" + quantity
							+ " where materialID='" + iMaterialID + "'";
					sql.Command(str);
					cm.logMaterial(1, "", "", iMaterialID, Double.parseDouble(quantity), iStation,
							"" , "NA", "595数量同步","NA");

					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5951, iStation + "_" + iMaterialID+"_"+quantity);
				} catch (Exception e) {
					str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(5950, iStation + "_" + iMaterialID+"_"+quantity);
				}
			}else if (msgClass.equals("620")) {//TODO 620松下物料无法准确反馈，需要MES主动监控
				String istr=MSG.asXML();
				Mroot.element("body").remove(Mroot.element("body").element("materials"));
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "0");
				eleR.addAttribute("errorText", "Valid");
				eleR.addAttribute("actionCode", "0");
				AKSLogs(6201, ""+istr );
			} else if (msgClass.equals("10000")) {// 申请条码
				iLayer = "2";
				iiLayer = Integer.parseInt(iLayer);
				iClient.orderActiveRequired = true;// 20210104修改为true，需要按工单确定工艺流程是否要求刻码
				String orderNo = null;
				try {
					orderNo = Mroot.element("body").element("PCBAssignReq").attribute("manufacturelotno").getValue();// iSN=modelNo
				} catch (Exception e) {
					;
				}
				if (orderNo == null) {
					orderNo = "";
				}
				Thread itt=null;//用于改变releaseFlag的子线程，防重码用
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					
					String strtt="select * from (\r\n" + 
							"select AAA.panelOrderNumber,AAA.orderIndex,AAA.planQty,AAA.addQty,count(1) as finishQty \r\n" + 
							"from (\r\n" + 
							"SELECT olp.panelOrderNumber,ol.orderIndex,ol.orderNumber,ol.planQty,ol.addQty,pS.isMarked \r\n" + 
							"FROM [dbo].[order_list_panel] olp \r\n" + 
							"LEFT JOIN [dbo].[order_list] ol ON ol.orderIndex=olp.orderIndex\r\n" + 
							"LEFT JOIN [dbo].[partStruct] pS ON pS.partIndex=ol.masterIndex \r\n" + 
							"where pS.isMarked=1 and olp.panelOrderNumber='"+orderNo+"' \r\n" + 
							"GROUP BY olp.panelOrderNumber,ol.orderIndex,ol.orderNumber,ol.planQty,ol.addQty,pS.isMarked \r\n" + 
							") AAA\r\n" + 
							"LEFT JOIN [dbo].[order_serialNumber] os ON os.orderIndex=AAA.orderIndex \r\n" + 
							"where os.releaseFlag=2\r\n" + 
							"GROUP BY AAA.panelOrderNumber,AAA.orderIndex,AAA.planQty,AAA.addQty\r\n" + 
							") BBB \r\n" + 
							"where finishQty>=(planQty+addQty) ";
					JTable t = sql.select(strtt);
					if(t!=null && t.getRowCount()>0) {//230719 addby Akite ：条码发行时小概率双线程同时发行导致同一工单同一时间发行两次
						throw new Exception("工单号" + orderNo + "条码申请失败,计划数量已达成");
					}
					strtt = "SELECT panelSN,pos,serialNumber FROM order_serialNumber "// 单表管控
							+ "where panelOrderNumber='" + orderNo + "' and releaseFlag=0 " + "ORDER BY panelSN,pos ";
					t = sql.select(strtt);
					if ((t == null) || (t.getRowCount() < 1)) {
						throw new Exception("工单号" + orderNo + "条码申请失败");
					} else {
						if ((iClient.activeOrderNumber == null) || (!iClient.activeOrderNumber.equals(orderNo))) {
//							sCl.changeStationOrder(orderNo, iiLayer);
							throw new Exception("工单号" + orderNo + "不是本工位当前激活工单");
						}
						int gmaxCounter=-1;
						String isttr="select ass.counter,ass.maxCounter,"
								+ "DATEDIFF(MINUTE, isnull(ass.lastCheck,getdate()), GETDATE()) as minuteLong,"
								+ "toli.timer,ass.lastCheck,getdate() as nowTime \r\n" + 
								"from [dbo].[assetStation] ass\r\n" + 
								"LEFT JOIN [QM].[timeOut_laserInspec] toli ON toli.timer is not null \r\n" + 
								"where ass.ID='"+iClient.myStationID+"' ";
						JTable t1=sql.select(isttr);
						if(t1!=null && t1.getRowCount()>0) {// counter>=maxCounter或当前时间超过巡检时间 报错
							int igCounter=Integer.parseInt(""+t1.getValueAt(0, 0));
							gmaxCounter=Integer.parseInt(""+t1.getValueAt(0, 1));
							if(gmaxCounter>0 && igCounter>=gmaxCounter) {
								throw new Exception("发码数量超过首检限制"+igCounter+"/"+gmaxCounter);
							}
							int igTlong=Integer.parseInt(""+t1.getValueAt(0, 2));
							int igMaxTlong=(int) Double.parseDouble(""+t1.getValueAt(0, 3));
							if(igMaxTlong>0 && igTlong>=igMaxTlong) {
								String igtl=""+t1.getValueAt(0, 4);
								String igmtl=""+t1.getValueAt(0, 5);
								throw new Exception("超过巡检时间限制"+igtl+"/"+igmtl+"("+igMaxTlong+")");
							}
						}
						String gPanelSN = (String) t.getValueAt(0, 0);
						int ggmaxCounter=gmaxCounter;
						if ((gPanelSN != null) && (gPanelSN.length() > 0)) {
							itt=new Thread(new Runnable() {
								int imaxCounter=ggmaxCounter;
								String mPanelSN = gPanelSN;
								String iigStationID=iClient.myStationID;
								@Override
								public void run() {
									boolean flag = true;
									try {
										flag = true;
										while (flag) {
											try {
												sql.Command("update order_serialNumber set releaseFlag=1 "
														+ "where panelSN='" + mPanelSN + "'");
												flag = false;
											} catch (Exception e) {
												flag = true;// 如果sql连接失败，重试
												Thread.yield();
												try {
													Thread.sleep(300);
												} catch (InterruptedException e1) {
													;
												}
											}
										}
										
//										if(imaxCounter>0) {
//											sql.Command("insert [QM].[temp10000_laserInspec] (stationID,panelSN) "
//													+ "values ('"+iigStationID+"','"+mPanelSN+"') ");
//											sql.Command("update [dbo].[assetStation] set counter=counter+1 where ID='"+iigStationID+"' ");
//										}//2023-03-24 经过首检5拼后仍然需要周期性验证
										sql.Command("insert [QM].[temp10000_laserInspec] (stationID,panelSN) "
												+ "values ('"+iigStationID+"','"+mPanelSN+"') ");
										sql.Command("update [dbo].[assetStation] set counter=counter+1 where ID='"+iigStationID+"' ");
									} catch (Exception e2) {
										;
									}
//									try {
//										isql.close();
//									}catch(Exception e1) {
//										;
//									}

								}
							});
							itt.start();
							Element eleMP = eleB.addElement("PCBMPAssignAck");
							eleMP.addAttribute("barcode", gPanelSN);
							eleMP.addAttribute("manufacturelotno", orderNo);
//							eleMP.addAttribute("position", "0");
							for (int i = 0; i < t.getRowCount(); i++) {
								String iiPanelSN = (String) t.getValueAt(i, 0);
								if (gPanelSN.equals(iiPanelSN)) {
									String iiPos = (String) t.getValueAt(i, 1);
									String iiSN = (String) t.getValueAt(i, 2);
									Element eleSP = eleMP.addElement("PCBSPAssignAck");
									eleSP.addAttribute("barcode", iiSN);
									eleSP.addAttribute("position", iiPos);
								} else {
									eleMP.addAttribute("subpanelcount", "" + i);
									i = t.getRowCount() + 1;
								}
							}
						} else {
							throw new Exception("工单号" + orderNo + "申请条码失败,拼板条码不能为空");
						}
						// 获取第一拼，同时把它releaseFlag改成1，表示isRelease
					}
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					if(itt!=null) {
						try {
							itt.join();
						} catch (Exception e) {
							AKSLogs(100002, iStation + "_order=" + orderNo+"release=1子线程.join报错:"+e.getMessage());
						}
					}
					AKSLogs(100001, iStation + "_order=" + orderNo);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100000, iStation + "_order=" + orderNo + ":_" + str);
				}
			} else if (msgClass.equals("10001")) {// 登记条码
				Element elePanel = Mroot.element("body").element("MPPCBTagReq");
				String panelSN = null;
				String panelResult = null;
				String iOrder=null;
				try {
					iOrder=elePanel.attribute("manufacturelotno").getValue();
					panelSN = elePanel.attribute("barcode").getValue();
					panelResult = elePanel.attribute("result").getValue();
				} catch (Exception e) {
					;
				}
				if (panelSN == null) {
					panelSN = "";
				}
				if (panelResult == null) {
					panelResult = "";
				}
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					iClient.orderActiveRequired = true;
//					iClient.getStationSetting(iStation);
					iClient.myLayer = 2;
					String gPanelSN = panelSN;
					boolean bState = false;
					if (panelResult.equals("0")) {
						bState = true;
					}
					bState = true;// 2020-12-18 由于有刻码机不传拼板结果，并且没有刻码机开启读码功能，修改为所有10001视为PASS
					if (bState) {
						String iOrderNumber=iOrder;
						new Thread(new Runnable() {
							String mPanelSN = gPanelSN;
							@Override
							public void run() {
								boolean flag = true;
								try {
									if(iOrderNumber!=null) {
										iClient.freshOrderWipQty_II(iOrderNumber);
									}
									flag = true;
									while (flag) {
										try {
											sql.Command("update order_serialNumber set releaseFlag=2 " + "where panelSN='"
													+ mPanelSN + "'");
											flag = false;
											if(iOrderNumber!=null) {
												iClient.freshOrderWipQty_II(iOrderNumber);
											}
										} catch (Exception e) {
											flag = true;// 如果sql连接失败，重试
											Thread.yield();
											try {
												Thread.sleep(300);
											} catch (InterruptedException e1) {
												;
											}
										}
									}
								} catch (Exception e2) {
									;
								}
								
							}
						}).start();
						iClient.uploadPanelState(panelSN, true,"-1");
					} else {
						new Thread(new Runnable() {
							String mPanelSN = gPanelSN;

							@Override
							public void run() {
								boolean flag = true;
								try {

									flag = true;
									while (flag) {
										try {
											sql.Command("update order_serialNumber set state=2 " + "where panelSN='"
													+ mPanelSN + "'");
											flag = false;
										} catch (Exception e) {
											flag = true;// 如果sql连接失败，重试
											Thread.yield();
											try {
												Thread.sleep(300);
											} catch (InterruptedException e1) {
												;
											}
										}
									}
								} catch (Exception e2) {
									;
								}
							}
						}).start();
					}
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100011, iStation + "_panel=" + panelSN);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100010, iStation + "_panel=" + panelSN + ":_" + str);
				}
			} else if (msgClass.equals("10003")) {// 设备验证
				String eqName = "";
				try {
					eqName = Mroot.element("body").element("equipment").attribute("name").getValue();
				} catch (Exception e) {
					eqName = iStation;
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleM = eleB.addElement("maintain");// <maintain state="1" nextTime="2017-08-31 14:00:00"
															// alert="3">
				try {
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					eleM.addAttribute("state", "1");
					eleM.addAttribute("nextTime", GetTimestamp("yyyy-MM-dd hh:mm:ss"));
					eleM.addAttribute("alert", "0");
					AKSLogs(100031, iStation + "_EQ=" + eqName);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					eleM.addAttribute("state", "1");
					eleM.addAttribute("nextTime", GetTimestamp("yyyy-MM-dd hh:mm:ss"));
					eleM.addAttribute("alert", "0");
					AKSLogs(100030, iStation + "_EQ=" + eqName + ":_" + str);
				}
			} else if (msgClass.equals("10004")) {// 人员验证
				String iUser = Mroot.element("body").element("login").attribute("userid").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleA = eleB.addElement("authority");
				try {
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					eleA.addAttribute("value", "4");
					AKSLogs(100041, iStation + "_user=" + iUser);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					eleA.addAttribute("value", "0");
					AKSLogs(100040, iStation + "_user=" + iUser + ":_" + str);
				}
			} else if (msgClass.equals("10009")) {// 新建包装箱
				String iNumber = Mroot.element("body").element("product").attribute("number").getValue();
				String iVersion = Mroot.element("body").element("product").attribute("version").getValue();
				String iBIN = Mroot.element("body").element("product").attribute("bin").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleA = eleB.addElement("packageContainer");
				try {
					c_package c = new c_package(sql);
					String boxNumber = c.newBox(iNumber, iVersion, iBIN, iStation);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					eleA.addAttribute("number", boxNumber);
					AKSLogs(100091, iStation + "_newBox=" + iNumber + "_" + iVersion + "_" + iBIN);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					eleA.addAttribute("number", "NA");
					AKSLogs(100090, iStation + "_newBox=" + iNumber + "_" + iVersion + "_" + iBIN);
				}
			} else if (msgClass.equals("10010")) {// 包装箱进出产品
				String iNumber = Mroot.element("body").element("product").attribute("number").getValue();
				String iBox = Mroot.element("body").element("packageContainer").attribute("number").getValue();
				String iIO = Mroot.element("body").element("packageContainer").attribute("type").getValue();
				String iOP = Mroot.element("body").element("packageContainer").attribute("operator").getValue();
				String iStatus = Mroot.element("body").element("packageContainer").attribute("state").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					c_package c = new c_package(sql);
					int i = 0;
					if (iIO.equals("1")) {
						i = 1;
					}
					c.packageIO(i, iNumber, iBox, iOP, iStatus);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100101, iStation + "_BoxIO=" + iNumber + "_" + iBox + "_" + iIO + "_" + iOP);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100100, iStation + "_BoxIO=" + iNumber + "_" + iBox + "_" + iIO + "_" + iOP);
				}
			} else if (msgClass.equals("10011")) {// 获取包装箱信息
				String iNumber = Mroot.element("body").element("product").attribute("number").getValue();
				String iWipLabel = Mroot.element("body").element("product").attribute("label").getValue();
				String iBox = Mroot.element("body").element("packageContainer").attribute("number").getValue();
				String iBoxLabel = Mroot.element("body").element("packageContainer").attribute("label").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					c_package c = new c_package(sql);
					/**
					 * result:<BR>
					 * 0=boxNumber<BR>
					 * 1=label<BR>
					 * 2=partNumber<BR>
					 * 3=version<BR>
					 * 4=bin<BR>
					 * 5=state<BR>
					 * 6=customerNo<BR>
					 * 7=maxQty<BR>
					 * 8=controlLevel<BR>
					 * 9=quantity<BR>
					 */
					String[] boxInfo = c.getPackageBoxInfo(iNumber, iWipLabel, iBox, iBoxLabel, iStation);// packageContainer
					Element eleP = eleB.addElement("packageContainer");
					int i = 0;
					eleP.addAttribute("boxNumber", boxInfo[i++]);
					eleP.addAttribute("label", boxInfo[i++]);
					eleP.addAttribute("partNumber", boxInfo[i++]);
					eleP.addAttribute("version", boxInfo[i++]);
					eleP.addAttribute("bin", boxInfo[i++]);
					eleP.addAttribute("state", boxInfo[i++]);
					eleP.addAttribute("customerNo", boxInfo[i++]);
					eleP.addAttribute("capacity", boxInfo[i++]);
					eleP.addAttribute("controlLevel", boxInfo[i++]);
					eleP.addAttribute("quantity", boxInfo[i++]);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100111, iStation + "_BoxInfo=" + iNumber + "_" + iWipLabel + "_" + iBox + "_" + iBoxLabel);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100110, iStation + "_BoxInfo=" + iNumber + "_" + iWipLabel + "_" + iBox + "_" + iBoxLabel);
				}
			} else if (msgClass.equals("10012")) {// 包装箱状态变更
				String iBox = Mroot.element("body").element("packageContainer").attribute("number").getValue();
				String iLabel = Mroot.element("body").element("packageContainer").attribute("label").getValue();
				String iStatus = Mroot.element("body").element("packageContainer").attribute("state").getValue();
				String iOP = Mroot.element("body").element("packageContainer").attribute("operator").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				try {
					c_package c = new c_package(sql);
					int iiStatus = Integer.parseInt(iStatus);
					c.setPackageState(iBox, iLabel, iiStatus, iOP);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100121, iStation + "_setBoxState=" + iBox + "_" + iStatus + "_" + iOP);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100120, iStation + "_setBoxState=" + iBox + "_" + iStatus + "_" + iOP);
				}
			} else if (msgClass.equals("10013")) {// 获取产品BIN描述信息
				String partNumber, version, bin, orderNumber;
				try {
					partNumber = Mroot.element("body").element("product").attribute("number").getValue();// 产品编号
				} catch (Exception e) {
					partNumber = "";
				}
				try {
					version = Mroot.element("body").element("product").attribute("version").getValue();// 产品BIN
				} catch (Exception e) {
					version = "";
				}
				try {
					bin = Mroot.element("body").element("product").attribute("bin").getValue();// 产品BIN
				} catch (Exception e) {
					bin = "";
				}
				try {
					orderNumber = Mroot.element("body").element("order").attribute("number").getValue();// 工单号
				} catch (Exception e) {
					orderNumber = "";
				}

				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleP = eleB.addElement("product");

				String sqlstr = "";

				try {
					if ((partNumber == null || partNumber.length() < 1) || (version == null || version.length() < 1)
							|| (bin == null || bin.length() < 1)) {
						if (orderNumber == null || orderNumber.length() < 1) {
							throw new Exception("查询条件不全，必须要有工单编号或者完整的产品信息");
						} else {
							sqlstr = "select masterOrderNumber,partNumber,version,BSindex from vOrderList_new where orderNumber='"
									+ orderNumber + "'";
							JTable t = sql.select(sqlstr);
							if (t != null && t.getRowCount() > 0) {
								String iOrder = (String) t.getValueAt(0, 0);
								if (!iOrder.equals(orderNumber)) {
									orderNumber = iOrder;
									sqlstr = "select masterOrderNumber,partNumber,version,BSindex from vOrderList_new where orderNumber='"
											+ orderNumber + "'";
									t = sql.select(sqlstr);
								}
								partNumber = (String) t.getValueAt(0, 1);
								version = (String) t.getValueAt(0, 2);
								bin = (String) t.getValueAt(0, 3);
							} else {
								sqlstr = "SELECT olp.panelOrderNumber,bvs.panelNumber,bvs.panelVersion,olp.panelBIN \r\n" + 
										"FROM [dbo].[order_list_panel] olp \r\n" + 
										"LEFT JOIN dbo.order_list ol ON ol.orderIndex=olp.orderIndex \r\n" + 
										"LEFT JOIN BOM.vStruct bvs ON bvs.panelNumber=olp.panelNumber and bvs.partIndex=ol.masterIndex and bvs.partVersion=ol.version\r\n" + 
										"where olp.panelOrderNumber='"+orderNumber+"' and ol.orderNumber!=ol.masterOrderNumber ";
								t = sql.select(sqlstr);
								if (t != null && t.getRowCount() > 0) {
									partNumber = (String) t.getValueAt(0, 1);
									version = (String) t.getValueAt(0, 2);
									bin = (String) t.getValueAt(0, 3);
								}else {
									throw new Exception("工单号不存在");
								}
							}
						}
					}
					if ((partNumber == null || partNumber.length() < 1) || (version == null || version.length() < 1)
							|| (bin == null || bin.length() < 1)) {
						throw new Exception("产品BIN号描述基础信息不存在");
					} else {
						int binIndex = Integer.parseInt(bin);
						sqlstr = "select desc1,desc2,desc3,desc4 from product_BINpartViewer where masterNumber='" + partNumber
								+ "' and version='" + version + "' and BSIndex=" + binIndex;
						JTable t = sql.select(sqlstr);
						String bin_laser = "", bin_label = "", bin_package = "" ,bin_FCT="";// 多种LED的情况
						int irow = t.getRowCount();
						for (int i = 0; i < irow; i++) {
							if (i > 0) {
								bin_laser = bin_laser + ",";
								bin_label = bin_label + ",";
								bin_package = bin_package + ",";
								bin_FCT = bin_FCT + ",";
							}
							bin_laser = bin_laser + (String) t.getValueAt(i, 0);
							bin_label = bin_label + (String) t.getValueAt(i, 1);
							bin_package = bin_package + (String) t.getValueAt(i, 2);
							bin_FCT = bin_FCT + (String) t.getValueAt(i, 3);
						}

						eleP.addAttribute("number", partNumber);
						eleP.addAttribute("version", version);
						eleP.addAttribute("bin", bin);
						eleP.addAttribute("bin_laser", "" + bin_laser);
						eleP.addAttribute("bin_label", "" + bin_label);
						eleP.addAttribute("bin_package", "" + bin_package);
						eleP.addAttribute("bin_FCT", "" + bin_FCT);
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(100131, "" + partNumber + "_" + version + "_" + bin + "_" + iStation);
					}

				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(100130, "" + partNumber + "_" + version + "_" + bin + "_" + iStation + "_" + str);
				}
			} else if (msgClass.equals("10015")) {// 设备验证
				String iMachine = Mroot.element("body").element("equipment").attribute("name").getValue();
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				Element eleM = eleB.addElement("maintain");
				Element eleE = eleB.addElement("equipment");

				try {
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					eleM.addAttribute("state", "1");
					eleM.addAttribute("nextTime", "");
					eleM.addAttribute("alert", "null");
					eleE.addAttribute("type", "GW_001");
					eleE.addAttribute("remaincount", "99999");
					eleE.addAttribute("maxcount", "99999");
					eleE.addAttribute("alarmcount", "0");
					eleE.addAttribute("measurestate", "0");
					eleE.addAttribute("repairstate", "0");
					AKSLogs(100151, iStation + "_machine=" + iMachine);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					eleM.addAttribute("state", "1");
					eleM.addAttribute("nextTime", "");
					eleM.addAttribute("alert", "null");
					eleE.addAttribute("type", "GW_001");
					eleE.addAttribute("remaincount", "99999");
					eleE.addAttribute("maxcount", "99999");
					eleE.addAttribute("alarmcount", "0");
					eleE.addAttribute("measurestate", "0");
					eleE.addAttribute("repairstate", "0");
					AKSLogs(100150, iStation + "_machine=" + iMachine + ":_" + str);
				}
			} else if (msgClass.equals("100006")) {// 获取BOM
				String orderNo = null;
				try {
					orderNo = Mroot.element("body").element("orderInfo").attribute("order_no").getValue();// iSN=modelNo
				} catch (Exception e) {
					;
				}
				if (orderNo == null) {
					orderNo = "";
				}
				iLayer = Mroot.element("body").element("orderInfo").attribute("layer").getValue();
				iiLayer = Integer.parseInt(iLayer);

				Element eleB = Mroot.element("body");
				Element eleR = eleB.addElement("result");
				try {
					
					if (orderNo.length() < 1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", "工单号不能为空");
						AKSLogs(100060, "order=" + orderNo + ":_工单号不能为空");
					} else {
						m_orderBOM result = new c_order(sql).getOrderBOM(orderNo, iiLayer);// 按照工单面别获取BOM

						Element eleBOM = eleB.addElement("BOM");
						if (result.panelNumber != null) {
							if (result.panelNumber.size() > 0) {
								Element elePanels = eleBOM.addElement("panels");
								for (int i = 0; i < result.panelNumber.size(); i++) {
									elePanels.addElement("panel").addAttribute("number", result.panelNumber.get(i));
								}
							}
						}
						if (result.masterNumber != null) {
							if (result.masterNumber.size() > 0) {
								Element masters = eleBOM.addElement("masters");
								for (int i = 0; i < result.masterNumber.size(); i++) {
									Element eleMaster = masters.addElement("master");
									eleMaster.addAttribute("number", result.masterNumber.get(i));
									eleMaster.addAttribute("version", result.masterVersion.get(i));
									String iBIN = "" + result.BSindex.get(i);
									while (iBIN.length() < 3) {
										iBIN = "0" + iBIN;
									}
									eleMaster.addAttribute("BIN", iBIN);
									eleMaster.addAttribute("name", result.masterName.get(i));
								}
							}
						}
						if (result.partNumber != null) {
							if (result.partNumber.size() > 0) {
								Element eleParts = eleBOM.addElement("parts");
								for (int i = 0; i < result.partNumber.size(); i++) {
									Element elePart = eleParts.addElement("part");
									elePart.addAttribute("number", result.partNumber.get(i));
									elePart.addAttribute("version", result.partVersion.get(i));
									elePart.addAttribute("name", result.partName.get(i));
									elePart.addAttribute("pos", result.partPos[i]);
									elePart.addAttribute("master", result.partBABA.get(i));
									elePart.addAttribute("panel", result.partMAMA.get(i));
								}
							}
						}
						if (result.comps != null) {
							if (result.comps.size() > 0) {
								Element eleComps = eleBOM.addElement("comps");
								for (int i = 0; i < result.comps.size(); i++) {
									if (result.comps.get(i).procedure != null
											&& result.comps.get(i).procedure.equals(iClient.myProcedureNumber)) {
										Element eleComp = eleComps.addElement("comp");
										eleComp.addAttribute("part", result.comps.get(i).compBABA);
										eleComp.addAttribute("layer", "" + result.comps.get(i).layer);
										eleComp.addAttribute("pos", result.comps.get(i).name);
										eleComp.addAttribute("number", result.comps.get(i).materialNumber);
										eleComp.addAttribute("name", result.comps.get(i).materialName);
									}
								}
							}
						}

						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						AKSLogs(100061, "order=" + orderNo + "_" + iLayer);
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					AKSLogs(100060, "order=" + orderNo + ":_" + str);
				}
			} else if (msgClass.equals("10101")) {// 核对SN的BIN是否一致
//				iSN=Mroot.element("body").element("pcb").attribute("barcode").getValue();
				@SuppressWarnings("unchecked")
				List<Element> pcbs = Mroot.element("body").elements();
				String SNs = "";
				for (int i = 0; i < pcbs.size(); i++) {
					if (i > 0) {
						SNs = SNs + ",";
					}
					SNs = SNs + "'" + pcbs.get(i).attribute("barcode").getValue() + "'";
				}
				SNs = "(" + SNs + ")";

				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				String strsql = "select BSindex from vOrderSerialNumber where serialNumber in " + SNs
						+ " group by BSindex";

				try {
					JTable t = sql.select(strsql);
					if (t == null || t.getRowCount() < 1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", "条码不存在");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(101010, "" + SNs + "_条码不存在");
					} else if (t.getRowCount() > 1) {
						eleR.addAttribute("errorCode", "1");
						eleR.addAttribute("errorText", "多种BIN");
						eleR.addAttribute("actionCode", transactionID);
						AKSLogs(101010, "" + SNs + "_多种BIN");
					} else {
						eleR.addAttribute("errorCode", "0");
						eleR.addAttribute("errorText", "Valid");
						eleR.addAttribute("actionCode", "0");
						AKSLogs(101011, "" + SNs);
					}
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(101010, "" + SNs + "_" + str);
				}
			}else if (msgClass.equals("10021")) {// TODO 人员信息查询
				
				String userID =Mroot.element("body").element("user").attribute("number").getValue();
				Mroot.element("body").remove(Mroot.element("body").element("user"));
				Element eleR = Mroot.element("body").addElement("result");
				Element eleA = Mroot.element("body").addElement("authority");
				Element eleU = Mroot.element("body").addElement("user");
				
				try {
					
					
					//TODO 功能转移中，临时回复固定值，含义是技能等级，数字大的覆盖数字小的能力
					eleA.addAttribute("value", "4");
					eleA.addAttribute("starttime", "2012-01-01 00:00:00 000");//iFormat="yyyy-MM-dd HH:mm:ss SSS";
					eleA.addAttribute("endtime", "2112-01-01 00:00:00 000");
					eleA.addAttribute("alarmtime", "2112-01-01 00:00:00 000");
					eleU.addAttribute("number", userID);
					eleU.addAttribute("name", "功能转移中");
					eleU.addAttribute("state", "01");
					//<!--state =01enrollment：在岗 02resignation:离职-->
					
					
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100211, "" + iStation + "_" + userID);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(100210, "" + iStation + "_" + userID);
				}
			}else if (msgClass.equals("10022")) {// TODO 人员登录
				Element eleR = Mroot.element("body").addElement("result");
				Element eleL=Mroot.element("body").element("login");
				String userID =eleL.attribute("userid").getValue();//userid : 员工编号  
				String itype =eleL.attribute("type").getValue();//type:1 login in 0 login out
				
				Mroot.element("body").remove(Mroot.element("body").element("login"));
				
				try {
					
					//TODO 一人一岗制
					sql.Command("update assetStation set [operator]='"+userID+"' where ID='"+iStation+"'");
					
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100221, "" + iStation + "_" + userID);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", transactionID);
					AKSLogs(100220, "" + iStation + "_" + userID);
				}
			} else if (msgClass.equals("10025")) {// 获取SN信息
				iSN = Mroot.element("body").element("pcb").attribute("barcode").getValue();
				Mroot.element("body").remove(Mroot.element("body").element("pcb"));
				Element eleR = Mroot.element("body").addElement("result");
				Element eleW = Mroot.element("body").addElement("wipno");

				try {
					m_SNinfo result = iClient.getSNinfo(iSN);// 如果工序是A0200强制重回FCT，除非没有FCT工序
					if (result == null) {
						isUnknowCase = true;
						return null;
//						throw new Exception("条码信息不存在");	// 查不到的去32
					}
					JTable t=sql.select("select createTime from order_list where orderNumber='"+result.lotNo+"'");
					if(t!=null && t.getRowCount()>0) {
						result.createTime=""+t.getValueAt(0, 0);
					}
					eleW.addAttribute("referencewipno", result.panelWipno);
					eleW.addAttribute("subwipno", iSN);
					eleW.addAttribute("position", "" + result.position);
					eleW.addAttribute("referencelotno", result.panelLotNo);
					eleW.addAttribute("sublotno", result.lotNo);
					eleW.addAttribute("sublotdate",result.createTime);
					eleW.addAttribute("product", result.number);
					eleW.addAttribute("bin", result.bin);
					eleW.addAttribute("bin_laser", result.led1);
					eleW.addAttribute("bin_label", result.led2);
					eleW.addAttribute("bin_package", result.led3);
					eleW.addAttribute("bin_FCT", result.led4);
					eleW.addAttribute("productName", result.name);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(100251, "" + iSN + "_" + iStation);
				} catch (Exception e) {
					isUnknowCase = true;
					return null;
				}
			} else if (msgClass.equals("100003")) {// 发布工单SN
				String lv2Key = "orderInfo";
				String orderNo = null;
				orderNo = Mroot.element("body").element(lv2Key).attribute("order_no").getValue();// iSN=modelNo
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				if (orderNo == null) {
					orderNo = "-1";
				}
				try {
					orderManagement om = new orderManagement(sql);
					if (orderNo.equals("-1")) {
						om.createSN4orders();
					} else {
						om.createSN4order(orderNo);
					}
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(1000031, "" + orderNo + "_" + iStation);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(1000030, orderNo + "_" + iStation + ":_" + str);
				}
			} else if (msgClass.equals("100004")) {// 按<拼板>工单获取相关物料信息
				/**
				 * 1.按拼板工单获取子板工单 2.获取合并标记 ORD_WOBinDet 3.获取所有相关工单 4.开线程下载物料信息
				 */
				String lv2Key = "orderInfo";
				String orderNo = null;
				orderNo = Mroot.element("body").element(lv2Key).attribute("order_no").getValue();// iSN=modelNo
				Mroot.remove(Mroot.element("body"));
				Element eleB = Mroot.addElement("body");
				Element eleR = eleB.addElement("result");
				if (orderNo == null) {
					orderNo = "-1";
				}
				try {
					String[] pickOrder = null;
					Vector<String> inputs = new Vector<String>();
					isPanel = false;
					String oOrder = orderNo;
					Thread iTH = new Thread(new Runnable() {
						String orderNo = oOrder;
						@Override
						public void run() {
							String str = "select masterOrderNumber from vOrderList_new where panelOrderNumber='"
									+ orderNo + "' group by masterOrderNumber";
							try {
								JTable t = sql.select(str);
								if (t != null && t.getRowCount() > 0) {
									isPanel = true;
									for (int i = 0; i < t.getRowCount(); i++) {
										String iOrder = (String) t.getValueAt(i, 0);
										inputs.add(iOrder);
									}
								}
							} catch (Exception e) {
								;
							}
						}
					});

					Thread iTH1 = new Thread(new Runnable() {
						String orderNo = oOrder;
						@Override
						public void run() {
							String str = "select masterOrderNumber from vOrderList_new where masterOrderNumber='"
									+ orderNo + "' group by masterOrderNumber";
							try {
								JTable t = sql.select(str);
								if (t != null && t.getRowCount() > 0) {
									for (int i = 0; i < t.getRowCount(); i++) {
										String iOrder = (String) t.getValueAt(i, 0);
										inputs.add(iOrder);
									}
								}
							} catch (Exception e) {
								;
							}
						}
					});
					iTH.start();
					iTH1.start();

					iTH.join();
					if (isPanel == false) {
						iTH1.join();
					}

					if (inputs.isEmpty()) {
						throw new Exception("工单" + orderNo + "不存在");
					}

					pickOrder = new c_material(sql).getPickListOrders_mergeNo(inputs);
					if (pickOrder == null) {
						throw new Exception("WMS工单" + inputs + "不存在");
					}
					Vector<String> sonOrders = new Vector<String>();
					String strPickOrders = "";
					for (int i = 0; i < pickOrder.length; i++) {
						if (pickOrder[i] != null && pickOrder[i].length() > 0 && sonOrders.indexOf(pickOrder[i]) < 0) {
							sonOrders.add(pickOrder[i]);
							if (sonOrders.size() == 1) {
								strPickOrders = "'" + pickOrder[i] + "'";
							} else {
								strPickOrders = strPickOrders + ",'" + pickOrder[i] + "'";
							}
						}
					}
					strPickOrders = "(" + strPickOrders + ")";
					new c_material(sql).getPickResult_groups(strPickOrders);
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "Valid");
					eleR.addAttribute("actionCode", "0");
					AKSLogs(1000041, "" + orderNo + "_" + iStation);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					eleR.addAttribute("actionCode", "0");
					AKSLogs(1000040, orderNo + "_" + iStation + ":_" + str);
				}
			} else {
				isUnknowCase = true;
				return null;
			}
			endT = GetTimestamp();
			diffMS = getDiffMS(startT1, endT);
//			if(diffMS>500) { //TODO 调试时启用
//				System.out.println(threadName+"【" + msgClass + "】__接口时间" + diffMS);
//			}
			if (diffMS > 3 * 1000) {
				AKSLogs(-105,
						"AKS_transactionID=" + transactionID + "," + System.getProperty("line.separator") + "msgClass="
								+ msgClass + "," + System.getProperty("line.separator") + "totalTime=" + diffMS
								+ "ms" + System.getProperty("line.separator") + detailLog);
			}
			return MSG;
		} catch (Exception e) {
			if(iClient==null) {
				Element Mroot = MSG.getRootElement();
				Element eleR = Mroot.element("body").addElement("result");
				eleR.addAttribute("errorCode", "1");
				eleR.addAttribute("errorText", "工位信息获取失败");
				eleR.addAttribute("actionCode", "0");
				return MSG;
			}
			String str = errCodeManagement.getErrText(e.getMessage());
			System.out.println(str);
			return null;
		}

	}

	private m_panelSonResultState getErrCompsInfo(m_panelSonResultState iState, List<?> errComps) {
		if(errComps==null || errComps.size()<1) {
			return iState;
		}
		iState.errComps=new String[errComps.size()];
		iState.errCode=new String[errComps.size()];
		for (int n = 0; n < errComps.size(); n++) {
			String isnRpass = "0";
			try {
				isnRpass = ((Element) errComps.get(n)).attribute("bRpass").getValue();
			}catch(Exception e) {
				isnRpass = ((Element) errComps.get(n)).attribute("Rpass").getValue();
			}
			if (isnRpass.equals("0")) {//TODO 240509 改为0的按照不良品获取 
				iState.errComps[n]=((Element) errComps.get(n)).attribute("comps").getValue();
				iState.errCode[n]=((Element) errComps.get(n)).attribute("errCode").getValue();
			}
			
		}
		return iState;
	}

	/**
	 * 	获取在加严清单中的位号
	 * @param iPos 
	 * @param t
	 * @return
	 * @throws Exception 
	 */
	private Vector<String> getSPIxRayListComps(String iPos) throws Exception {

		Vector<String> iXRComps = new Vector<String>();
		String iMasterNumber = null;
		iMasterNumber=sql.selectFirstValue(
				"select masterNumber from vOrderListPanel where sequence=" + iPos
						+ " and panelOrderNumber='" + iClient.activeOrderNumber + "'");
		if (iMasterNumber != null && iMasterNumber.length() > 0) {// 查询总成号
			JTable t = sql.select(
					"select partNumber,comps,materialNumber from SPIxRayList where partNumber='"
							+ iMasterNumber + "'");
			if (t != null && t.getRowCount() > 0) {// 下载加严清单中该总成的所有位号
				for (int m = 0; m < t.getRowCount(); m++) {
					String strXR = "" + t.getValueAt(m, 1);
					if (strXR != null && strXR.length() > 0
							&& (!strXR.equals("null"))) {
						iXRComps.addAll(Arrays.asList(strXR.split(",")));
					}
				}
				
			}

		}
		
		return iXRComps;
	}

	/**
	 * 	获取有复判的位号
	 * @param errComps
	 * @return
	 */
	private Vector<String> getRepairComps(List<?> errComps) {
		Vector<String> iRpComps = new Vector<String>();
		for (int n = 0; n < errComps.size(); n++) {
			String isnRpass = "0";
			try {
				isnRpass = ((Element) errComps.get(n)).attribute("bRpass").getValue();
			}catch(Exception e) {
				isnRpass = ((Element) errComps.get(n)).attribute("Rpass").getValue();
			}
			if (isnRpass.equals("1")) {//231201 从0改为1，SPI中，1代表RPASS
				String[] strComps = ((Element) errComps.get(n)).attribute("comps")
						.getValue().split(",");
				iRpComps.addAll(Arrays.asList(strComps));
			}
		}
		return iRpComps;
	}

	private Thread uploadPCBs(String iStation,String iSN, String iLayer, String iState, 
			String iRepair,String transactionID, Element eleR) throws Exception {
		String iiSN=iSN;
		Thread iTH1 = new Thread(new Runnable() {
			String oSN=iiSN;
			@Override
			public void run() {
				eleR.addAttribute("pcbID", oSN);
				try {
					boolean isget = false;
					int iiState=0;
					try {
						iiState = Integer.parseInt(iState);
						isget = true;
					}catch(Exception e) {
						String str = errCodeManagement.getErrText("过站电文异常body->panel->state_"+e.getMessage());
						throw new Exception(str);
					}
					String oLayer=iLayer.replaceAll(" ", "");
					int iiLayer = Integer.parseInt(oLayer);
					if(iiLayer==-1) {
						iiLayer=iClient.myLayer;
					}
					if (iiLayer != iClient.myLayer) {
						String str = "工单激活面别(" + iClient.myLayer + ")与产品上传面别不一致！";
						throw new Exception(str);
					} else {
						boolean bState = false;
						if (iiState == 0) {
							bState = true;
						}
						boolean bRepair = false;
						if (iRepair.equals("1")) {
							bRepair = true;
						}
						boolean icallXRay = false;
						try {
							iClient.uploadState(oSN, bState, bRepair, icallXRay,"-1");// 如果工序是A0200强制重回FCT，除非没有FCT工序
						} catch (Exception ex) {
							if (ex.getMessage().equals("-152")) {
								String realSN = iClient.getSN4Label(oSN);// 如果条码未识别，先查询看看是否为标签
								if (realSN != null) {
									oSN = realSN;
									iClient.uploadState(oSN, bState, bRepair, icallXRay,"-1");
								} else {
									throw ex;
								}
							} else {
								throw ex;
							}
						}
					}
					
					eleR.addAttribute("errorCode", "0");
					eleR.addAttribute("errorText", "");
					AKSLogs(5501, "" + oSN + "_" + iStation + "_" + iLayer);
				} catch (Exception e) {
					String str = errCodeManagement.getErrText(e.getMessage());
					eleR.addAttribute("errorCode", "1");
					eleR.addAttribute("errorText", str);
					AKSLogs(5500, "" + oSN + "_" + iStation + "_" + iLayer + "_" + str);
				}
				eleR.addAttribute("actionCode", transactionID);
			}
		});
		iTH1.start();
		
		return iTH1;
		
		
		
	}

	public void AKSLogs(int iType, String ilogClientNews) {
		SQLtool.AKSLogs(iType, ilogClientNews);
	}
	
	public String getStationOrder(String iStation) throws Exception {
		if(iStation.endsWith("NPM-M2")) {// 应对M2不激活工单的特殊设定
			iStation=iStation.replaceAll("-M2", "-M1");
		}
		String str="select orderNumber from assetStation where ID='"+iStation+"'";
		JTable t=sql.select(str);
		String oOrder="";
		if(t!=null && t.getRowCount()>0) {
			oOrder=""+t.getValueAt(0, 0);
		}else {
			if(iStation.endsWith("M2")) {// 应对M2不激活工单的特殊设定
				iStation=iStation.replaceAll("-M2", "-M1");
			}
			str="select orderNumber from assetStation where ID='"+iStation+"'";
			t=sql.select(str);
			if(t!=null && t.getRowCount()>0) {
				oOrder=""+t.getValueAt(0, 0);
			}
		}
		return oOrder;
	}

	public static String GetTimestamp() {
		// "2017-12-10 15:52:10";
		String timestamp = new String();
		SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		timestamp = "" + t.format(new Date());
		return timestamp;
	}

	public static String GetTimestamp(String iFormat) {
		// "2017-12-10 15:52:10";
		if(iFormat == null || iFormat.length()<1 ) {
			iFormat="yyyy-MM-dd HH:mm:ss SSS";
		}
		String timestamp = new String();
		SimpleDateFormat t = new SimpleDateFormat(iFormat);
		timestamp = "" + t.format(new Date());
		return timestamp;
	}

	public static long getDiffMS(String iStartT, String iEndT) {
		SimpleDateFormat t = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss SSS");
		try {
			Date s = t.parse(iStartT);
			Date e = t.parse(iEndT);
			long diff = e.getTime() - s.getTime();
			if (diff < 0) {
				diff = diff * (-1);
			}
			return diff;
		} catch (ParseException e1) {
			System.out.print(e1.getMessage());
			;
		}
		return -1;
	}
}
